I have a dataframe where I want to reduce its size by selected all instances TRUE appears in dataframe.
Here is the dataframe:
df<-structure(c("1", "2", "3", "4", "5", "TRUE", "FALSE", "TRUE",
"TRUE", "FALSE", "FALSE", "FALSE", "FALSE", "TRUE", "FALSE",
"TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "a", "b", "c", "d",
"e"), .Dim = c(5L, 5L), .Dimnames = list(NULL, c("A", "B_down",
"C_down", "D_down", "E")))
To reduce the dataframe to where TRUE is, I used this code:
df[which(apply(df[,c(2:4)],1,function(x) any(x)=="TRUE")),]
However, I manually selected columns c(2:4) - B_down, C_down, D_down, as they have _down ending. How do I choose these columns dynamically in R, without hard coding it.
I see in a [post here] (filtering with multiple conditions on many columns using dplyr), one can use select(df, ends_with("_down")), but this only gives me a partial dataframe. I want the whole dataframe structure to be maintained as above.
Thank you for your help.