简体   繁体   中英

R load CSV with # in headers

I'm trying to load a csv file that has symbol '#' in the header. 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

Any ideas on how to resolve the problem?

Thanks

Try 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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