从csv文件创建命名向量不起作用

时间:2017-09-19 04:34:58

标签: r

创建命名向量,其中名称与csv文件中的GO id相关联不起作用。

> head(read.delim("~/GOmapping.tsv", sep = '\t'))
         V1                              V14
1 sp0000005                       GO:0003723
2 sp0000006                       GO:0016021
3 sp0000007 GO:0003700,GO:0006355,GO:0043565
4 sp0000016                       GO:0046983
5 sp0000017 GO:0004672,GO:0005524,GO:0006468
6 sp0000022            GO:0003677,GO:0046983

> head(read.delim("~/GOmapping.tsv", sep = '\t'))[1]
         V1
1 sp0000005
2 sp0000006
3 sp0000007
4 sp0000016
5 sp0000017
6 sp0000022

> head(read.delim("~/GOmapping.tsv", sep = '\t'))[2]
                               V14
1                       GO:0003723
2                       GO:0016021
3 GO:0003700,GO:0006355,GO:0043565
4                       GO:0046983
5 GO:0004672,GO:0005524,GO:0006468
6            GO:0003677,GO:0046983
> geneID2GO <- read.delim("~/GOmapping.tsv", sep = '\t'))[2]


> geneID2GO <- read.delim("~/GOmapping.tsv", sep = '\t')[2]
> names(geneID2GO) <- read.delim("~/GOmapping.tsv", sep = '\t')[1]


> head(geneID2GO)
  c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 57, 58, 59, 60, 6 ...
1                                                                                                                                                                                                                                                     GO:0003723
2                                                                                                                                                                                                                                                     GO:0016021
3                                                                                                                                                                                                                               GO:0003700,GO:0006355,GO:0043565
4                                                                                                                                                                                                                                                     GO:0046983
5                                                                                                                                                                                                                               GO:0004672,GO:0005524,GO:0006468
6                                                                                                                                                                                                                                          GO:0003677,GO:0046983

我错过了什么?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您想要一个矢量作为结果,也许您可​​以尝试将您的值和名称(第1列)强制转换为字符。

data <- read.delim("~/GOmapping.tsv", sep = '\t')
geneID2GO <- as.character(data[,2])
names(geneID2GO) <- as.character(data[,1])
head(geneID2GO)
                    sp0000005                          sp0000006                          sp0000007 
                  "GO:0003723"                       "GO:0016021" "GO:0003700,GO:0006355,GO:0043565" 
                     sp0000016 
                  "GO:0046983" 

或者,您可以按如下方式显示结果:

cbind(geneID2GO)
          geneID2GO                         
sp0000005 "GO:0003723"                      
sp0000006 "GO:0016021"                      
sp0000007 "GO:0003700,GO:0006355,GO:0043565"
sp0000016 "GO:0046983"