Cannot read file with “#” and space using read.table or read.csv in R

亡梦爱人 提交于 2019-11-27 03:56:01

问题


I have a file where the first row is a header. The header can have spaces and the # symbol (there may be other special characters as well). I am trying to read this file using read.csv or read.table but it keeps throwing me errors:

undefined columns selected 

more columns than column names 

My tab-delimited chromFile file looks like:

Chromosome# Chr chr Size    UCSC NCBI36/hg18    NCBIBuild36 NCBIBuild37
1   Chr1    chr1    247199719   247249719   247249719   249250621
2   Chr2    chr2    242751149   242951149   242951149   243199373

Command:

chromosomes <- read.csv(chromFile, sep="\t",skip =0, header = TRUE,  )

I want to first look for a way to read the file as it as without replacing the space or # with some other readable symbol.


回答1:


From the documentation (?read.csv):

comment.char character: a character vector of length one containing a single character or an empty string. Use "" to turn off the interpretation of comments altogether.

The default is comment.char = "#" which is causing you trouble. Following the documentation, you should use comment.char = "".

Spaces in the header is another issue which, as mrdwab kindly pointed out, can be addressed by setting check.names = FALSE.

chromosomes <- read.csv(chromFile, sep = "\t", skip = 0, header = TRUE,
                        comment.char = "", check.names = FALSE)


来源:https://stackoverflow.com/questions/12771522/cannot-read-file-with-and-space-using-read-table-or-read-csv-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!