无法读取带有“#"的文件在 R 中使用 read.table 或 read.csv 和空间 [英] Cannot read file with "#" and space using read.table or read.csv in R

查看:29
本文介绍了无法读取带有“#"的文件在 R 中使用 read.table 或 read.csv 和空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中第一行是标题.标题可以有空格和# 符号(也可能有其他特殊字符).我正在尝试使用 read.csv 或 read.table 读取此文件,但它不断向我抛出错误:

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 

我的制表符分隔的 chromFile 文件如下所示:

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

命令:

chromosomes <- read.csv(chromFile, sep="	",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.

推荐答案

来自文档 (?read.csv):

comment.char 字符:长度为 1 的字符向量,包含单个字符或空字符串.使用"完全关闭评论的解释.

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.

默认是 comment.char = "#" 这会给你带来麻烦.按照文档,您应该使用 comment.char = "".

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

标题中的空格是另一个问题,正如 mrdwab 友好指出的那样,可以通过设置 check.names = FALSE 来解决.

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 = "	", skip = 0, header = TRUE,
                        comment.char = "", check.names = FALSE)

这篇关于无法读取带有“#"的文件在 R 中使用 read.table 或 read.csv 和空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆