I have data.frame in R; he is called p. Each item in data.frame is True or False. My variable phas, say, m rows and n columns. There is only one element for each row TRUE.
It also has column names that are strings. I would like to do the following:
- For each row in
pI see a TRUE, which I would like to replace with the name of the corresponding column - I would like to collapse the data.frame file, which now contains
FALSEcolumn names, to one element that will contain m elements. - I would like to do this with the help of R-thonic, to continue my enlightenment in R and contribute to a world without for-loops.
I can do step 1 using the following loop:
for (i in seq(length(colnames(p)))) {
p[p[,i]==TRUE,i]=colnames(p)[i]
}
but there is no beauty here, and I completely subscribed to this for-loop-in-R - this is probably the wrong mentality. Perhaps wrong too much, but they are certainly not great.
I really donβt know how to take step 2. I kind of hoped that the sum of the string FALSEwould return the string, but it is not. I kind of hoped that I could use some kind of OR operator, but I couldn't figure it out (Python responds False or 'bob'to 'bob'). Therefore, again, I turn to you with beautiful Rstats people for help!
source
share