简体   繁体   English

R在标题中使用#加载CSV

[英]R load CSV with # in headers

I'm trying to load a csv file that has symbol '#' in the header. 我正在尝试加载在标头中包含符号“#”的csv文件。 This is causing an error message 'More Columns than Column Names'. 这导致出现错误消息“列数多于列名”。 I've followed some the articles ( here & here ), but I can not manage to sort it out. 我已经关注了一些文章( 此处此处 ),但是我无法对其进行整理。

The problem seems to be that the upload excludes columns after the #. 问题似乎是上载排除了#号之后的列。 For example, I've created a file with the following column headers (no rows) 例如,我创建了一个具有以下列标题(无行)的文件

A   B#  C # D

When I upload I get the following: 当我上传时,我得到以下信息:

hTest <- read.table("C:/Users/xxx/headerTest.csv",header=T, sep = ',',check.names=FALSE)
hTest
[1] A B
<0 rows> (or 0-length row.names)

As you can see, after the # in column B the following columns are ignored 如您所见,在B列中的#之后,以下各列将被忽略

Any ideas on how to resolve the problem? 关于如何解决问题的任何想法?

Thanks 谢谢

Try comment.char = '' 尝试comment.char = ''

df1 <- read.table("temp.txt", header = TRUE, sep = "\t", comment.char = '')

You will get a . 你会得到一个. in place of # . 代替# After reading the file contents, you can edit the column names using colnames() function. 读取文件内容后,可以使用colnames()函数编辑列名。

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

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