我有一个深层嵌套数据和一个通过purrr在其上运行的函数。这是一个模拟数据&分析类似于我的情况:
df <- tibble::tribble(
~A, ~B, ~C,
"e", 2L, 6L,
"e", 5L, 8L,
"e", 5L, 3L,
"f", 3L, 8L,
"f", 4L, 1L,
"f", 5L, 6L,
"g", 3L, 9L,
"g", 4L, 2L,
"g", 5L, 7L,
"h", 5L, 4L
)
我需要根据不同的条件过滤不同的变量。我需要做这样的事情:
df1 <- df %>% group_by(A) %>%
nest() %>%
case_when(A == "e" ~filter(B<4),
A == "f" ~filter(C<=6),
A == "g" ~filter(B<5, C<7))
我想要的输出应该是:
desired_output <- tibble::tribble(
~A, ~B, ~C,
"e", 2L, 6L,
"f", 4L, 1L,
"f", 5L, 6L,
"g", 4L, 2L
)
答案 0 :(得分:4)
我们可以执行以下操作来过滤不同的条件。
library(tidyverse)
df %>%
filter((A %in% "e" & B < 4) |
(A %in% "f" & C <= 6) |
(A %in% "g" & B < 5 & C < 7))
# # A tibble: 4 x 3
# A B C
# <chr> <int> <int>
# 1 e 2 6
# 2 f 4 1
# 3 f 5 6
# 4 g 4 2
答案 1 :(得分:3)
选项可以是:
df1 <- df %>% group_by(A) %>%
mutate(filterV = case_when(
A == "e" ~ (B<4),
A == "f" ~ (C<=6),
A == "g" ~ (B<5 & C<7))) %>%
filter(filterV) %>%
select(-filterV)
df1
# # A tibble: 4 x 3
# # Groups: A [3]
# A B C
# <chr> <int> <int>
# 1 e 2 6
# 2 f 4 1
# 3 f 5 6
# 4 g 4 2