gpt4 book ai didi

R过滤出现多次但不包含特定字符串的数据

转载 作者:行者123 更新时间:2023-12-02 18:12:26 26 4
gpt4 key购买 nike

我有一个数据框如下。我想首先按 ID 和区域对数据框进行分组,然后按内容过滤数据框。我想保留“内容”列中仅包含“高”和“中”的 ID-区域对。

我当前的数据框:

structure(list(ID = c("a", "a", "a", "a", "a", "b", "b", "c", 
"c", "c", "c", "d", "d", "d"), Content = c("High", "Mid", "Low",
"High", "Mid", "High", "Mid", "High", "Mid", "High", "Mid", "High",
"Mid", "Low"), Count = c("3", "6", "2", "3", "7", "3", "5", "8",
"5", "4", "2", "4", "9", "5"), Region = c("East", "East", "East",
"West", "West", "East", "East", "East", "East", "West", "West",
"West", "West", "West")), class = "data.frame", row.names = c(NA,
-14L))

我期望的数据框:

structure(list(ID = c("a", "a", "b", "b", "c", "c", "c", "c"), 
Content = c("High", "Mid", "High", "Mid", "High", "Mid",
"High", "Mid"), Count = c("3", "7", "3", "5", "8", "5", "4",
"2"), Region = c("West", "West", "East", "East", "East",
"East", "West", "West")), class = "data.frame", row.names = c(NA,
-8L))

我尝试以下代码,但无法保留仅具有“高”和“中”的 ID-区域对。

newdata = data %>%
group_by(ID,Region) %>%
filter(n() != 1 & any(Content == "High") & any(Content == "Mid") & any(Content != "Low") %>%
ungroup

最佳答案

我们可以过滤以保留不包含任何 Content == Low 并且必须包含 HighMid 的任何组。

library(dplyr)

df %>%
group_by(ID,Region) %>%
filter(!any(Content == "Low") & all(c("High", "Mid") %in% Content) )

输出

  ID    Content Count Region
<chr> <chr> <chr> <chr>
1 a High 3 West
2 a Mid 7 West
3 b High 3 East
4 b Mid 5 East
5 c High 8 East
6 c Mid 5 East
7 c High 4 West
8 c Mid 2 West

关于R过滤出现多次但不包含特定字符串的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72092664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com