使用if语句和条件进行过滤:dplyr管道
我试图过滤我的数据,这样,在某些日期,我有太多的观察,我想削减其中一些。例如,在下面的数据中,我对使用if语句和条件进行过滤:dplyr管道,r,dplyr,R,Dplyr,我试图过滤我的数据,这样,在某些日期,我有太多的观察,我想削减其中一些。例如,在下面的数据中,我对2005-03-31164次观察的结果有点扭曲。我所做的是计算一些排序方法,从1:5开始。对于“太多”观测值的观测值,我只对顶部的整数感兴趣,这就是为什么我按余弦整数分组,并创建了更多的整数,称为余弦整数 现在我只对cosine\u ntile中的第1个部分感兴趣,例如,如果观察次数超过100次。我尝试的是过滤器(total\u obs>100&cosine\u ntile==1),它没有给出所需的
2005-03-31
164次观察的结果有点扭曲。我所做的是计算一些排序方法,从1:5开始。对于“太多”观测值的观测值,我只对顶部的整数感兴趣,这就是为什么我按余弦整数
分组,并创建了更多的整数,称为余弦整数
现在我只对cosine\u ntile
中的第1个部分感兴趣,例如,如果观察次数超过100次。我尝试的是过滤器(total\u obs>100&cosine\u ntile==1)
,它没有给出所需的输出
我正在尝试筛选总obs
是否大于100,然后删除余弦分量
列中得分为2、3、4或5的观测值
数据如下所示:
# A tibble: 200 x 4
filing_date_EoM total_obs cosine_ntile cosine_ntile_ntile
<date> <int> <dbl> <int>
1 2005-07-31 6 1 4
2 2005-09-30 15 4 2
3 2005-03-31 164 1 4
4 2005-12-31 14 2 3
5 2005-11-30 5 5 4
6 2005-03-31 164 4 3
7 2005-12-31 14 5 5
8 2005-12-31 14 2 4
9 2005-03-31 164 1 5
10 2005-02-28 17 3 3
编辑:预期输出:
filing_date_EoM total_obs cosine_ntile cosine_ntile_ntile
1 2005-07-31 6 1 4
2 2005-09-30 15 4 2
3 2005-03-31 164 1 4 ~Gets removed
4 2005-12-31 14 2 3
5 2005-11-30 5 5 4
6 2005-03-31 164 4 3 ~Gets removed
7 2005-12-31 14 5 5
8 2005-12-31 14 2 4
9 2005-03-31 164 1 5 ~Gets removed
10 2005-02-28 17 3 3
11 2005-06-30 13 2 4
12 2005-05-31 5 3 3
13 2005-03-31 163 5 3 ~Gets removed
14 2005-06-30 13 5 4
15 2005-02-28 17 5 1
16 2005-11-30 5 2 1
17 2005-03-31 164 2 3 ~Gets removed
18 2005-03-31 164 2 1 ~STAYS
19 2005-04-30 13 4 3
20 2005-12-31 14 4 1
21 2005-11-30 6 1 5
22 2005-03-31 164 2 4 ~Gets removed
23 2005-03-31 164 1 3 ~Gets removed
24 2005-09-30 15 4 2
25 2005-03-31 163 5 2 ~Gets removed
26 2005-05-31 5 1 4
27 2005-03-31 164 1 5 ~Gets removed
28 2005-02-28 17 1 2
29 2005-03-31 164 1 2 ~Gets removed
30 2005-02-28 17 3 1
31 2005-01-31 4 1 1
32 2005-03-31 164 3 5 ~Gets removed
33 2005-07-31 5 2 4
34 2005-03-31 164 1 3 ~Gets removed
35 2005-03-31 164 3 4 ~Gets removed
36 2005-09-30 16 1 1
37 2005-03-31 164 4 1 ~STAYS
38 2005-04-30 13 2 3
39 2005-03-31 164 1 4 ~Gets removed
40 2005-12-31 15 1 4
41 2005-09-30 15 4 3
42 2005-06-30 14 3 2
43 2005-08-31 4 4 3
44 2005-09-30 15 3 4
45 2005-04-30 13 4 1
观测值
18
之所以保留,是因为总观测值
>100,并且它处于余弦分量
=1。在第一个条件下失败的其他观察结果(即<100)也会保留(或未采取措施)。我们可以创建过滤器并进行否定(!
)
你能显示预期的输出吗?你需要df1%>%过滤器吗(!(total\u obs>100&cosine\u ntile!=1))
当然,我现在就写一些东西。当total\u obs
>100I添加更多信息时,只需删除cosine\u ntile
中的观察值5,4,3,2
。
filing_date_EoM total_obs cosine_ntile cosine_ntile_ntile
1 2005-07-31 6 1 4
2 2005-09-30 15 4 2
3 2005-03-31 164 1 4 ~Gets removed
4 2005-12-31 14 2 3
5 2005-11-30 5 5 4
6 2005-03-31 164 4 3 ~Gets removed
7 2005-12-31 14 5 5
8 2005-12-31 14 2 4
9 2005-03-31 164 1 5 ~Gets removed
10 2005-02-28 17 3 3
11 2005-06-30 13 2 4
12 2005-05-31 5 3 3
13 2005-03-31 163 5 3 ~Gets removed
14 2005-06-30 13 5 4
15 2005-02-28 17 5 1
16 2005-11-30 5 2 1
17 2005-03-31 164 2 3 ~Gets removed
18 2005-03-31 164 2 1 ~STAYS
19 2005-04-30 13 4 3
20 2005-12-31 14 4 1
21 2005-11-30 6 1 5
22 2005-03-31 164 2 4 ~Gets removed
23 2005-03-31 164 1 3 ~Gets removed
24 2005-09-30 15 4 2
25 2005-03-31 163 5 2 ~Gets removed
26 2005-05-31 5 1 4
27 2005-03-31 164 1 5 ~Gets removed
28 2005-02-28 17 1 2
29 2005-03-31 164 1 2 ~Gets removed
30 2005-02-28 17 3 1
31 2005-01-31 4 1 1
32 2005-03-31 164 3 5 ~Gets removed
33 2005-07-31 5 2 4
34 2005-03-31 164 1 3 ~Gets removed
35 2005-03-31 164 3 4 ~Gets removed
36 2005-09-30 16 1 1
37 2005-03-31 164 4 1 ~STAYS
38 2005-04-30 13 2 3
39 2005-03-31 164 1 4 ~Gets removed
40 2005-12-31 15 1 4
41 2005-09-30 15 4 3
42 2005-06-30 14 3 2
43 2005-08-31 4 4 3
44 2005-09-30 15 3 4
45 2005-04-30 13 4 1
library(dplyr)
df2 <- df1 %>%
filter(!(total_obs > 100 & cosine_ntile_ntile != 1))
df2 %>%
filter(total_obs > 100, cosine_ntile_ntile != 1)
# A tibble: 0 x 4
# … with 4 variables: filing_date_EoM <date>, total_obs <int>, cosine_ntile <dbl>, cosine_ntile_ntile <int>
subset(df1, !(total_obs > 100 & cosine_ntile_ntile != 1))