【发布时间】:2018-07-03 20:08:49
【问题描述】:
我被困住了。如何使用dplyr's filter()同时将我们两个关系运算符作为组内组内的过滤器@
我得到了什么
数据,
# install.packages(c("tidyverse"), dependencies = TRUE)
library(tibble)
tbl <- structure(list(id1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), id2 = c("x_02", "x_02", "x_02", "x_02", "x_02", "x_02",
"x_02", "x_02", "x_02", "x_02", "x_02", "x_02", "x_02", "x_03", "x_03", "x_03",
"x_03", "x_03", "x_03", "x_03", "x_03"), x = c(-4L, -3L, -2L, -1L, 1L, 2L, 3L,
4L, 5L, -2L, -1L, 1L, 2L, -2L, -1L, 1L, 2L, 3L, 4L, 5L, 6L)),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -21L),
vars = c("id1", "id2"), drop = TRUE, .Names = c("id1", "id2", "x"),
indices = list(0:8, 9:12, 13:20), group_sizes = c(9L, 4L, 8L),
biggest_group_size = 9L, labels = structure(list(id1 = c(1L, 2L, 2L),
id2 = c("x_02", "x_02", "x_03")), class = "data.frame", row.names = c(NA, -3L),
vars = c("id1", "id2"), drop = TRUE, .Names = c("id1", "id2")))
tbl
#> # A tibble: 21 x 3
#> # Groups: id1, id2 [3]
#> id1 id2 x
#> <int> <chr> <int>
#> 1 1 x_02 -4
#> 2 1 x_02 -3
#> 3 1 x_02 -2
#> 4 1 x_02 -1
#> 5 1 x_02 1
#> 6 1 x_02 2
#> 7 1 x_02 3
#> 8 1 x_02 4
#> 9 1 x_02 5
#> 10 2 x_02 -2
#> # ... with 11 more rows
简而言之,我想在id1 和id2 中查找,并找到从x < -2 开始到x > 2 结束的一系列xs(期望的结果下面的 mig 比我在这里描述的更好)。
在某种程度上我认为它是两个过滤器的组合,即我想要这个过滤器,
library(dplyr)
tbl %>% group_by(id1, id2) %>%
filter( (row_number() == n() & x > 2 ) )
#> # A tibble: 2 x 3
#> # Groups: id1, id2 [2]
#> id1 id2 x
#> <int> <chr> <int>
#> 1 1 x_02 5
#> 2 2 x_03 6
要与这个过滤器结合,
tbl %>% group_by(id1, id2) %>%
filter( (row_number() == 1 & x < -2 ) )
#> # A tibble: 1 x 3
#> # Groups: id1, id2 [1]
#> id1 id2 x
#> <int> <chr> <int>
#> 1 1 x_02 -4
我想是这样的,但这并没有给我任何数据。
tbl %>% group_by(id1, id2) %>%
filter( (row_number() == n() & x > 2 ) &
(row_number() == 1 & x < -2 ) )
为什么不给?
我想要得到/想要的结果
# A tibble: 2 x 3
# Groups: id1, id2 [1]
id1 id2 x
<int> <chr> <int>
1 1 x_02 -4
1 1 x_02 5
【问题讨论】:
-
您能解释一下为什么您在示例中的条件与您提供的解释不符吗?例如,您说您想在 id1 和 id2 中查找并找到从 > -2 开始并在 -2 相反声明:filter((row_number() == n() & x > 2))。我根本看不出你提到的条件和你的预期结果之间的关系。
-
@StewartRoss,谢谢您的提问。如果我理解正确,调用
row_number() == n()给我最后一个,n()th,行,row_number()内,group_by()。在这种情况下是id1和id2。然后我将它与条件&结合起来,即特定的最后一行必须大于该行中的 2,即x > 2。这就是我在数据后的第一次调用中所做的。结果是id111 和 2 和id2的调用是x_02和x_03值 5 和 6。我希望能回答你的问题?如果没有请再问。可以看看我的期望结果。谢谢。 -
您的代码要求在每个组中保留最后一行且是第一行且 x > 2 且 x
-
我已经更新了我的文本(就在数据下方)。你说其中一定没有。我不确定我是否同意。对于
id1 == 1组id2 == x_02中的情况,有一系列x值从-4开始并以5结束,这正是我想要提取的第一个和最后一个值。这就是我试图在我的期望结果中展示的内容。我想我可能错过了什么?请随时提出您认为可以澄清问题的任何修改。 -
你试过
filter( (row_number() == n() & x > 2 ) | (row_number() == 1 & x < -2 ) )吗?
标签: r filter dplyr conditional relational