R: manipulating data.frames containing strings and booleans.

Posted by Mike Dewar on Stack Overflow See other posts from Stack Overflow or by Mike Dewar
Published on 2010-04-21T16:24:58Z Indexed on 2010/04/21 16:43 UTC
Read the original article Hit count: 289

Filed under:

Hello. I have a data.frame in R; it's called p. Each element in the data.frame is either True or False. My variable p has, say, m rows and n columns. For every row there is strictly only one TRUE element.

It also has column names, which are strings. What I would like to do is the following:

  1. For every row in p I see a TRUE I would like to replace with the name of the corresponding column
  2. I would then like to collapse the data.frame, which now contains FALSEs and column names, to a single vector, which will have m elements.
  3. I would like to do this in an R-thonic manner, so as to continue my enlightenment in R and contribute to a world without for-loops.

I can do step 1 using the following for loop:

for (i in seq(length(colnames(p)))) {
    p[p[,i]==TRUE,i]=colnames(p)[i]
}

but theres's no beauty here and I have totally subscribed to this for-loops-in-R-are-probably-wrong mentality. Maybe wrong is too strong but they're certainly not great.

I don't really know how to do step 2. I kind of hoped that the sum of a string and FALSE would return the string but it doesn't. I kind of hoped I could use an OR operator of some kind but can't quite figure that out (Python responds to False or 'bob' with 'bob'). Hence, yet again, I appeal to you beautiful Rstats people for help!

© Stack Overflow or respective owner

Related posts about r