[R] Sharing levels across multiple factor vectors
Henrique Dallazuanna
wwwhsd at gmail.com
Thu Apr 1 15:35:45 CEST 2010
Try this also:
df[] <- as.numeric(unlist(df))
df
On Thu, Apr 1, 2010 at 2:53 AM, Jeff Brown <dopethatwantscash at yahoo.com> wrote:
>
> Sorry for spamming. I swear I had worked on that problem a long time before
> posting.
>
> But I just figured it out: I have to change the values, which are
> represented as integers, not strings. So the following code will do it:
>
> df <- data.frame (
> a = factor( c( "bob", "alice", "bob" ) ),
> b = factor( c( "kenny", "alice", "alice" ) )
> );
> allLevels <- unique( c( levels( df$a ), levels( df$b ) ) )
> for (c in colnames(df)) {
> df[,c] <- match( df[,c], allLevels);
> levels( df[,c] ) <- 1:(length(allLevels))
> };
>
> --
> View this message in context: http://n4.nabble.com/Sharing-levels-across-multiple-factor-vectors-tp1747714p1747722.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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list