[R] Header in read.table() function

Marc Schwartz marc_schwartz at me.com
Wed Feb 3 22:26:58 CET 2010


On Feb 3, 2010, at 3:08 PM, Paul Evans wrote:

> Hi,
> 
> I wanted to read in a table that had hyphens in the header / column names. When I read it in however, it replaces the hyphens with a dot. Which parameter in the read.table function do I need to set to change this behaviour?
> Example code:
> jm <- matrix(1:4,2,2)
> rownames(jm) <- c('a','b')
> colnames(jm) <- c('a-1','a-2')
> write.table(jm,'tjm.out',row.names=T,col.names=T,sep='\t')
> 
> mm <- read.table('tjm.out',row.names=1,header=T,sep='\t',colClasses = "character")
> print(mm)
>  a.1 a.2
> a   1   3
> b   2   4
> I would like 'a-1' 'a-2' in the header and not a.1 & a.2
> thanks.

You can set the check.names argument to FALSE, but I would strongly advise against this.

Depending upon the context of use, a-1 will be interpreted by R as the value(s) in object 'a' minus 1.

This is why there are legal object names/characters in R and it checks for them when importing data.

To get around the above default behavior, you can use quotes and such, but you are asking to get bitten by hard to find errors in your code if you proceed.

Caveat Emptor.

HTH,

Marc Schwartz



More information about the R-help mailing list