英文:
Retain Value if it's the Only One in Group, Otherwise Filter Out
问题
让我们假设我有一个看起来像这样的数据框:
dat <- data.frame(account = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
group = c("a", "a", "b", "b", "c", "c", "c", "d", "d"),
status = c("active", "inactive", "inactive", "inactive", "active", "open", "inactive", "active", "active"))
我想要进行筛选,以便每个组(a
、b
或c
)至少有一个帐户。因此,只有状态为active
或open
的帐户被保留,除非一个组中唯一的状态是inactive
。
如何进行这种筛选呢?
我认为可以进行多次调用,但是否有一种一行的方法呢?
dat_clean <- dat %>%
group_by(group) %>%
filter(all(status != "inactive") | any(status %in% c("active", "open"))) %>%
ungroup()
这种方法会根据组筛选出至少有一个active
或open
状态的帐户,除非该组中唯一的状态是inactive
。这可能是一种更简洁的方法。
英文:
Let's say I have a dataframe that looks like:
dat <- data.frame(account = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
group = c("a", "a", "b", "b", "c", "c", "c", "d", "d"),
status = c("active", "inactive", "inactive", "inactive", "active", "open", "inactive", "active", "active"))
And I want to filter such that each group (a
, b
, or c
) has at least one account. So only accounts where the status is active
or open
are kept, unless the only status in a group is inactive
.
How would I go about filtering that?
I think I could do it in multiple calls, but is there a way to do it in one line?
active_open <- dat %>%
group_by(group) %>%
filter(status == "active" | status == "open") %>%
ungroup()
inactive <- dat %>%
group_by(group) %>%
filter(n_distinct(status) == 1 & status == "inactive") %>%
slice_head(n = 1) %>%
ungroup()
dat_clean <- bind_rows(active_open, inactive)
This works, but I'm wondering if there's a cleaner way.
答案1
得分: 1
你可以尝试如下使用 filter
:
dat %>%
filter(
if (all(status == "inactive")) {
!duplicated(status)
} else {
status != "inactive"
}, .by = group)
这将得到以下结果:
account group status
1 1 a active
2 3 b inactive
3 5 c active
4 6 c open
英文:
You can try filter
like below
dat %>%
filter(
if (all(status == "inactive")) {
!duplicated(status)
} else {
status != "inactive"
}, .by = group)
which gives
account group status
1 1 a active
2 3 b inactive
3 5 c active
4 6 c open
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论