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:
- For every row in
p
I see aTRUE
I would like to replace with the name of the corresponding column - I would then like to collapse the data.frame, which now contains
FALSE
s and column names, to a single vector, which will have m elements. - 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!