Так много способов сделать это, также я думаю, что на это нужно где-то ответить
1) Используя colSums
df[colSums(df == "apple") > 0]
# V1 V3 V4 V7
#1 ball bird ball ball
#2 apple apple apple apple
#3 cat mouse cat cat
#4 mouse bat ball ball
#5 bat mouse bat bat
#6 bird ball bird bird
2) с apply
df[apply(df == "apple", 2, any)]
3) Использование Filter
Filter(function(x) any(x == "apple"), df)
4) dplyr
library(dplyr)
df %>% select_if(~any(. == "apple"))
data
df <- structure(list(V1 = structure(c(2L, 1L, 5L, 6L, 3L, 4L), .Label =
c("apple",
"ball", "bat", "bird", "cat", "mouse"), class = "factor"), V2 =
structure(c(3L,
4L, 2L, 1L, 1L, 1L), .Label = c("ball", "bat", "cat", "mouse"
), class = "factor"), V3 = structure(c(4L, 1L, 5L, 3L, 5L, 2L
), .Label = c("apple", "ball", "bat", "bird", "mouse"), class = "factor"),
V4 = structure(c(2L, 1L, 5L, 2L, 3L, 4L), .Label = c("apple",
"ball", "bat", "bird", "cat"), class = "factor"), V5 = structure(c(4L,
5L, 2L, 1L, 2L, 3L), .Label = c("ball", "bat", "bird", "cat",
"mouse"), class = "factor"), V6 = structure(c(4L, 5L, 2L,
1L, 2L, 3L), .Label = c("ball", "bat", "bird", "cat", "mouse"
), class = "factor"), V7 = structure(c(2L, 1L, 5L, 2L, 3L,
4L), .Label = c("apple", "ball", "bat", "bird", "cat"), class = "factor")),
class = "data.frame", row.names = c(NA, -6L))