[R] Matrix to data.frame with factors

arun smartpink111 at yahoo.com
Fri Oct 19 17:21:11 CEST 2012


Hi,
No problem.

You can also try this:
set.seed(1)
mat1<-matrix(sample(0:1,50,replace=TRUE),nrow=10,ncol=5)
 dat1<-as.data.frame(mat1)
 dat2<-do.call(data.frame,rapply(dat1,as.factor,classes="integer",how="replace"))
# str(dat2)
#'data.frame':    10 obs. of  5 variables:
# $ X1: Factor w/ 2 levels "0","1": 1 2 1 1 1 1 1 2 2 1
# $ X2: Factor w/ 2 levels "0","1": 2 1 1 1 2 1 1 2 1 2
# $ X3: Factor w/ 2 levels "0","1": 1 2 1 1 1 2 2 1 2 2
# $ X4: Factor w/ 2 levels "0","1": 1 2 1 1 2 1 2 1 1 1
# $ X5: Factor w/ 2 levels "0","1": 1 1 2 2 2 2 1 1 2 2
A.K.






----- Original Message -----
From: brunosm <brunosm87 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, October 19, 2012 10:15 AM
Subject: Re: [R] Matrix to data.frame with factors

Thanks a lot!



--
View this message in context: http://r.789695.n4.nabble.com/Matrix-to-data-frame-with-factors-tp4646730p4646756.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.





More information about the R-help mailing list