[R] Converting a matrix to a dataframe: how to prevent conversion to factor

From: Dennis Fisher <fisher_at_plessthan.com>
Date: Thu 08 Sep 2005 - 22:22:51 EST

Colleages

I am running R 2.1.0 on a Mac (same problem occurs in Linux). In some situations, I have mixed text/numeric data that is stored as characters in a matrix. If I convert this matrix to a dataframe, the numeric data becomes factors, not what I intend.

     TEXT    <- paste("Text", 1:4, sep="")
     NUMBERS    <- 10 + 4:1
     MATRIX    <- cbind(TEXT, NUMBERS)
     FRAME    <- as.data.frame(MATRIX)

> str(FRAME)

`data.frame': 4 obs. of 2 variables: $ TEXT : Factor w/ 4 levels "Text1","Text2",..: 1 2 3 4 $ NUMBERS: Factor w/ 4 levels "11","12","13",..: 4 3 2 1

One work-around is to write the matrix (or the dataframe) to a file, then read the file back using the as.is argument.

     write.table(MATRIX, "JUNK", row.names=F)
     NEWFRAME    <- read.table("JUNK", as.is=T, header=T)

> str(NEWFRAME)

`data.frame': 4 obs. of 2 variables: $ TEXT : chr "Text1" "Text2" "Text3" "Text4" $ NUMBERS: int 14 13 12 11

This restores the NUMBERS to their intended mode (integers, not factors). The text column is also not read as a factor (not a problem for me).

It appears that the function AsIs [I(x)] would enable me to accomplish this without the write/read steps. However, it is not obvious to me how to implement I(x). Can anyone advise?

Thanks in advance.

Dennis Fisher

Dennis Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-415-564-2220
www.PLessThan.com

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Sep 08 22:27:29 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:05 EST