考虑以下提示:
## # A tibble: 16 x 4
## name genus order sleep_total
## <chr> <chr> <chr> <dbl>
## 1 Cheetah Acinonyx Carnivora 12.1
## 2 Northern fur seal Callorhinus Carnivora 8.70
## 3 Vesper mouse Calomys Rodentia 7.00
## 4 Dog Canis Carnivora 10.1
## 5 Roe deer Capreolus Artiodactyla 3.00
## 6 Goat Capri Artiodactyla 5.30
## 7 Guinea pig Cavis Rodentia 9.40
## 8 Domestic cat Felis Carnivora 12.5
## 9 Gray seal Haliochoerus Carnivora 6.20
## 10 Tiger Panthera Carnivora 15.8
## 11 Jaguar Panthera Carnivora 10.4
## 12 Lion Panthera Carnivora 13.5
## 13 Caspian seal Phoca Carnivora 3.50
## 14 Genet Genetta Carnivora 6.30
## 15 Arctic fox Vulpes Carnivora 12.5
## 16 Red fox Vulpes Carnivora 9.80
我只想选择包含至少一个行值(称为“食肉目”)的列。
在这种情况下,预期输出为:
## order
## <chr>
## Carnivora
## Carnivora
## Rodentia
## Carnivora
## Artiodactyla
## Artiodactyla
## Rodentia
## Carnivora
## Carnivora
## Carnivora
## Carnivora
## Carnivora
## Carnivora
## Carnivora
## Carnivora
## Carnivora
其他人已经提供了如何获取包含该行的行。但是,这包括不包含该值的列。
sleep %>%
select(name:order, sleep_total, -vore) %>%
filter_all(any_vars(str_detect(., pattern = "Ca")))
答案 0 :(得分:3)
我们可以使用select_if
library(dplyr)
ggplot2::msleep %>% select_if(~any(. == "Carnivora", na.rm = TRUE))
# order
#1 Carnivora
#2 Carnivora
#3 Rodentia
#4 Carnivora
#5 Artiodactyla
#6 Artiodactyla
#7 Rodentia
#8 Carnivora
#9 Carnivora
#10 Carnivora
#11 Carnivora
#12 Carnivora
#13 Carnivora
#14 Carnivora
#15 Carnivora
#16 Carnivora
或者在基数R中使用colSums
msleep[colSums(msleep == "Carnivora", na.rm = TRUE) > 0]
或apply
msleep[apply(msleep == "Carnivora", 2, any, na.rm = TRUE)]
答案 1 :(得分:0)
我们可以将基数R中的%in%
与sapply
一起使用
library(ggplot2)
msleep[sapply(msleep, function(x) "Carnivora" %in% x)]