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.

______________________________________________
[email protected] 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.

Reply via email to