R: manipulating data.frames containing strings and booleans

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!

+3
source share
2 answers

Here are some sample data:

df <- data.frame(a=c(FALSE, TRUE, FALSE), b=c(TRUE, FALSE, FALSE), c=c(FALSE, FALSE, TRUE))

You can use applyto do something like this:

names(df)[apply(df, 1, which)]

Or without applyusing whichdirectly:

idx <- which(as.matrix(df), arr.ind=T)
names(df)[idx[order(idx[,1]),"col"]]
+4

apply :

> df <- data.frame(a=c(TRUE,FALSE,FALSE),b=c(FALSE,FALSE,TRUE),
+                  c=c(FALSE,TRUE,FALSE))
> df
      a     b     c
1  TRUE FALSE FALSE
2 FALSE FALSE  TRUE
3 FALSE  TRUE FALSE
> colnames(df)[apply(df, 1, which)]
[1] "a" "c" "b"
> 
+3

Source: https://habr.com/ru/post/1742130/


All Articles