[R] re ad.table, row.names arg

Adrian Dusa dusa.adrian at gmail.com
Fri Jun 5 19:55:33 CEST 2009




Markus Loecher-4 wrote:
> 
> Dear R users,
> I had somehow expected that read.table() would treat the column specified
> by
> the row.names argument as of class character. That seems to be the only
> sensible class allowed for a column containing row names. However, that
> does
> not seem to be the case, as the following example shows:
> 
>   x <- cbind.data.frame(ID = c("010007787048271871", "1007109516820319",
> "10094843652996959", "010145176274075487"), X1 = 1:4, X2 = 4:1)
> [...snip...]
> 

As a better alternative, why not move directly the first column in the
rownames?

rownames(x) <- x$ID
write.table(x[, -1], "tmp.txt")
y <- read.table("tmp.txt", header=T)
y
                   X1 X2
010007787048271871  1  4
1007109516820319    2  3
10094843652996959   3  2
010145176274075487  4  1

In this case, X1 and X2 variables are read as numeric, while the first
column is read as character and assigned directly to the rownames.

HTH,
Adrian
-- 
View this message in context: http://www.nabble.com/read.table%2C-row.names-arg-tp23888975p23892826.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list