繁体   English   中英

读取多个文件时R中的未定义列选择错误

[英]Undefined columns selected error in R while reading multiple files

我的目录中有很多文件,我想读取所有文件并选择它们的第二列,然后将这些列作为矩阵的行放置,但是我遇到了奇怪的错误。

有人可以帮助我弄清楚我的代码出了什么问题吗?

这是我的努力:

#read all files in one directoy into R and select desired column
nm <- list.files(path="April/mRNA_expression/")
Gene_exp<-do.call(rbind, lapply(nm, function(x) read.table(file=x,header=TRUE, sep= ",")[, 2]))
save(Gene_exp, file="Path to folder")

我得到的错误是:

## Error in `[.data.frame`(read.table(file = x, header = TRUE, sep = ""),  :
## undefined columns selected*

要检查一下,实际上我的文件有2列,我这样做是:

b <- read.table("A.genes.normalized_results", sep="")
dim(b)
## [1] 20532     2

我的文本文件如下所示:

gene_id normalized_count
?|100130426 0.0000
?|100133144 10.6340
?|100134869 5.6790
?|10357 106.4628
?|10431 710.8902
?|136542    0.0000
?|155060    132.2883
?|26823 0.5098
?|280660    0.0000
?|317712    0.0000
?|340602    0.0000
?|388795    1.2745
?|390284    5.3527
?|391343    2.5489
?|391714    0.7647
?|404770    0.0000
?|441362    0.0000

更好的解决方案是仅在读取时导入第二列。 使用colClasses参数完全跳过第一个:

Gene_exp<-do.call(rbind, lapply(nm, function(x) read.delim(file=x,header=TRUE, colClasses=c('NULL', 'character'))))

我假设第二列是character 如果需要,将其更改为适当的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM