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. [[alternative HTML version deleted]]
On Feb 3, 2010, at 4: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.If you must, which will mean more typing on your part and probably lead to obscure errors, set check.names to FALSE: > mm <- read.table('tjm.out',row.names=1,header=T,sep='\t',colClasses = "character", check.names=FALSE) > print(mm) a-1 a-2 a 1 3 b 2 4> > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Heritage Laboratories West Hartford, CT
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.read.table is trying to create a data.frame. If you're data.frame had a column named 'a', what should> mm$a-1do? Print out mm$a-1, or subtract 1 from mm$a ? If you want your original matrix back (i.e., object of class matrix), you could just use save/load functions. I don't think it's possible to have a data.frame with those names, just as you can't do, say: a-1 <- 1:10 Erik I
Tena koe Paul a-1 is not valid syntax in a name hence the conversion. See ?make.names for more information. You could change the names after importing with read.table; e.g. names(whatever)[2] <- 'a-1' but you may regret it later. HTH ..... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Paul Evans > Sent: Thursday, 4 February 2010 10:09 a.m. > To: r-help at stat.math.ethz.ch > Subject: [R] Header in read.table() function > > 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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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