[R] apply and factor

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Aug 10 10:15:56 CEST 2001


Prof Brian D Ripley <ripley at stats.ox.ac.uk> writes:

> The following should work (and does in S, so is a bug in R):
> test2 <- df
> test2[] <- lapply(df, factor)  # fails in R

Oho: We got this from Johns original data frame code in which a part of 
[<-.data.frame is this 

    else if (nA == 2) {
        value <- i
        i <- j <- NULL
        has.i <- has.j <- FALSE
    }

but R always passes "value" as a named argument in replacements. We
did this for a reason, but it escapes me at the moment. It does
arguably lead to neater code: The "value <- i" line shouldn't be there
in R since i will be missing and value already be set.

> but we need
> 
> test3 <- as.data.frame(lapply(df, factor))

Better:

test2[TRUE] <- lapply(df, factor)

or even (ouch...)

test2[,] <- lapply(df, factor)

-- 
   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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list