2015-10-09 73 views
-1

我有一個大的矩陣,看起來像這樣的事情,更多的行和列只保留特定的列和參考列矩陣

--- CellName1 CellName2 CellName3 
Gene A 1   2  3 
Gene B 4   5  6 
Gene C 7   8  9 
Gene D 10   11  12 

此矩陣幾千行和列的延伸。我只需要大約13列的電子表格,我在.txt文件中列出了其標題

什麼是僅保留具有特定標題的列(文本文件中的那些列)並保留引用的最佳方法列(基因A,基因B等)?

我試圖讓一個字符串數組出列想要的名字,但我得到了一個意外的符號錯誤:

rsem.genes.tpm <- read.delim("~/Desktop/Desktop/rsem.genes.tpm.matrix", header=FALSE) 
View(rsem.genes.tpm) 
library("cluster", lib.loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library") 

wanted<-c("cy82-CD45-pos-2-C04-S508-comb」, "Cy74_CD45_A06_S390_comb」, 「Cy74_CD45_C07_S415_comb」, 「Cy74_CD45_H08_S476_comb」, 「cy53-1-CD45-pos-1-A03-S3-comb」, 「cy53-1-CD45-pos-2-A10-S970-comb」, 「cy53-1-CD45-pos-2-B03-S975-comb」, 「cy53-1-CD45-pos-2-B09-S981-comb」, 「cy53-1-CD45-pos-2-B12-S984-comb」, 「cy53-1-CD45-pos-2-C10-S994-comb」, 「cy53-1-CD45-pos-2-C12-S996-comb」, 「cy53-1-CD45-pos-2-D09-S1005-comb」, 「cy53-1-CD45-pos-2-D10-S1006-comb」, 「cy53-1-CD45-pos-2-H05-S1049-comb」, 「cy58-1-CD45-pos-A09-S585-comb」) # Concatenates character strings into a vector 
Error: unexpected symbol in "wanted<-c("cy82-CD45-pos-2-C04-S508-comb」, "Cy74_CD45_A06_S390_comb" 

我想,我想使用的保存和下降功能的一些組合,但我不知道如何實現它們

+2

這當然是由於這個''''應該是''''。 –

回答

0

讓我們創建一些示例數據:

set.seed <- 123 
df <- as.data.frame(matrix(sample(1:10,25,replace = TRUE),nrow = 5)) 
df$gene <- c(paste0('gene',LETTERS[1:5])) 

說你想只保留列:「V1」,「V2」和「V5」:

to.keep <- c('V1','V2','V5') 
> df[,c('gene',to.keep)] 
    gene V1 V2 V5 
1 Gene A 6 1 5 
2 Gene B 9 4 1 
3 Gene C 7 7 8 
4 Gene D 4 10 1 
5 Gene E 10 4 4