[R] matrix of 1,0's to a data.frame of factors

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Dec 7 00:18:39 CET 2004


"Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.ac.be> writes:

> Hi Rajarshi,
> 
> try this:
> 
> mat <- sample(0:1, 20, TRUE); dim(mat) <- c(5,4)
> mat[,1] <- 0; mat[,3] <- 1
> #########
> dat <- data.frame(mat)
> dat[] <- lapply(dat, function(x) factor(x, levels=c("0","1")))
> dat
> lapply(dat, levels)

A small style/speed  point: lapply(dat, factor, levels=0:1) will do.
  

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list