[R] Sharing levels across multiple factor vectors

Jeff Brown dopethatwantscash at yahoo.com
Thu Apr 1 20:27:41 CEST 2010


Wow, those are much more elegant.  Thanks!

Peter suggests:

df[] <- lapply(df, factor, levels=allLevels, labels=seq_along(allLevels)) 

Henrique suggests:

df[] <- as.numeric(unlist(df))

In both of those cases, why is the []  needed?  When I evaluate df vs. df[],
they both look the same, but apparently they have different meanings.  Is a
data frame internally represented as a list, and does df[] let you assign to
the elements of the list while maintaining the object's nature as a data
frame?

Moreover, Henrique, why is it that what you suggested works if done in one
line, but not in two?  That is, 

df[] <- unlist(df)
df[] <- as.numeric(df[])

gives an error at the second line: "Error: (list) object cannot be coerced
to type 'double'."
-- 
View this message in context: http://n4.nabble.com/Sharing-levels-across-multiple-factor-vectors-tp1747714p1748436.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list