考虑以下数据集:
data <- tibble(
group = rep(1:4, 40),
year = rep(1980:2019, 4),
col= rnorm(160)
)
我希望过滤数据,使得:
获取值在组1和2中大于零,在组3和4中小于零的子集,其中“col”是列名。
其中一种实现方式是:
data %>% filter(col > 0 & group %in% c(1,2) | col < 0 & group %in% c(3,4))
这里还有一种方法,直接使用数学方法进行选择,而不是使用%in%
data %>% filter(col * sign((group < 3) - 0.5) > 0)
#> # A tibble: 76 x 3
#> group year col
#> <int> <int> <dbl>
#> 1 2 1985 2.20
#> 2 3 1986 -0.205
#> 3 4 1991 -2.10
#> 4 3 1994 -0.113
#> 5 2 1997 1.90
#> 6 1 2000 1.37
#> 7 3 2002 -0.805
#> 8 4 2003 -0.535
#> 9 1 2004 0.792
#> 10 3 2006 -1.28
#> # ... with 66 more rows
library(dplyr) data %>% filter((group %in% 1:2 & col > 0) | (group %in% 3:4 & col < 0))
这段代码使用了dplyr包中的filter()
函数,对数据集进行筛选。根据条件,筛选出group
为1或2且col
大于0的行,或者group
为3或4且col
小于0的行。 - PaulSdata%>%filter(group <3&col> 0 | group> 2&col <0)
。 - lovalery