I have found multiple answers where one can select a row based on a value of a column, but I need the reverse. Consider this example:
library(dplyr)
df <- readr::read_table2("Location Site Species Date Time
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE FALSE TRUE FALSE
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE TRUE")
How can I get the columns or column names which have the values FALSE
, considering I know it is in the third row.
I tried something like this: df[3,df[3,]==FALSE]
, but this gives me an error.
Maybe something along the lines like this:
df <- readr::read_table2("Location Site Species Date Time
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE FALSE TRUE FALSE
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE TRUE")
which(df == FALSE, arr.ind = TRUE)[,2] #gives you the col numbers
The argument arr.ind
, if True, gives you the array indices from which you can extract the column numbers
You can use the following solution:
library(dplyr)
df %>%
select(where(~ any(. == FALSE))) %>%
names()
[1] "Species" "Time"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With