Re: [R] Sharing levels across multiple factor vectors

2010-04-02 Thread Jeff Brown
Ah, I finally figured it out: I had asked > In both of those cases, why is the [] needed? It's because when on the left hand side of an assignment, the bracket operator attempts to preserve the class and dimension of the object it's subsetting. (Or at least, that's true when the object is a

Re: [R] Sharing levels across multiple factor vectors

2010-04-01 Thread Jeff Brown
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 app

Re: [R] Sharing levels across multiple factor vectors

2010-04-01 Thread Henrique Dallazuanna
Try this also: df[] <- as.numeric(unlist(df)) df On Thu, Apr 1, 2010 at 2:53 AM, Jeff Brown 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 stri

Re: [R] Sharing levels across multiple factor vectors

2010-04-01 Thread hadley wickham
On Thu, Apr 1, 2010 at 3:05 AM, Peter Dalgaard wrote: > Jeff Brown 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 c

Re: [R] Sharing levels across multiple factor vectors

2010-04-01 Thread Peter Dalgaard
Jeff Brown 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(

Re: [R] Sharing levels across multiple factor vectors

2010-03-31 Thread Jeff Brown
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" ) ),

[R] Sharing levels across multiple factor vectors

2010-03-31 Thread Jeff Brown
Hi, I've got a data frame with multiple factor columns, but they should share the same set of labels, such as this tiny example: df <- data.frame ( a = factor( c( "bob", "alice", "bob" ) ), b = factor( c( "kenny", "alice", "alice" ) ) ); In my data, though, the strings are enorm