我有一个dataframe,其中我想将一个列子集为只包含与一个不同列中多个字符串相匹配的字符串。下面是一些模拟数据:
df1 <- data.frame(species = c("Rufl","Rufl","Soca","Assp","Assp","Elre"),
state = c("warmed","ambient","warmed","warmed","ambient","ambient"))我想要一个只有与“温暖”和“环境”状态相匹配的物种的数据have,删除只匹配一个字符串的物种,这样最后的数据have将有"Rufl“和"Assp”与它们的给定状态,如下所示
species state
Rufl warmed
Rufl ambient
Assp warmed
Assp ambient我尝试过几次不同的尝试,包括使用子集函数和dplyr,但是无法找到正确的方法来使其工作。以下是我失败的尝试:
df2 <- subset(df1$species, state == "warmed" & state == "ambient")
# or this?
df2 <- df1 %>%
group_by(species) %>%
filter(state == "warmed",
state == "ambient")谢谢你的帮助!
使用R版本4.0.2,Mac OS X 10.13.6
发布于 2021-04-30 16:36:19
我们需要一个all的小组
library(dplyr)
df1 %>%
group_by(species) %>%
filter(all(c('warmed', 'ambient') %in% state)) %>%
ungroup-output
# A tibble: 4 x 2
# species state
# <chr> <chr>
#1 Rufl warmed
#2 Rufl ambient
#3 Assp warmed
#4 Assp ambient&操作不工作,因为元素不存在于同一位置
或者使用subset
subset(df1, species %in% names(which(rowSums(table(df1) > 0) == 2)))发布于 2021-04-30 18:07:18
使用ave的另一个基本R选项
subset(
df1,
ave(state, species, FUN = function(x) sum(c("warmed", "ambient") %in% x)) == 2
)给出
species state
1 Rufl warmed
2 Rufl ambient
4 Assp warmed
5 Assp ambienthttps://stackoverflow.com/questions/67337458
复制相似问题