3
votes

I am using read.table in R to read in a file which has the following header:

ColA ColB# ColC ColD ColE

However, having a '#' in the header name confuses read.table and I get the following error:

*Error in read.table(paste(path, file, sep = ""), skip = SKIP_LINES, sep = "", : more columns than column names*

Any suggestions how I can get rid of the error message?

2

2 Answers

2
votes

try read.table(comment.char="?" ...) where what you use as a comment.char is something that isn't in your table:

read.table(
  comment.char="?", header=T, check.names=F,
  text="ColA ColB# ColC ColD ColE\n1 2 3 4 5"
)
#   ColA ColB# ColC ColD ColE
# 1    1     2    3    4    5
3
votes

To improve on BrodieG's answer, just in case there are random question marks ("?") in a file, it is proper convention to use comment.char="" when you want to ignore any and all comment characters.

read.table(comment.char="", header=T, check.names=F, text="ColA ColB# ColC ColD ColE\n1 2 3 4 5").

Which gives:

 ColA ColB# ColC ColD ColE
1    1     2    3    4    5