使用OR时,过滤器不能处理数据帧(dplyr)

时间:2016-06-24 02:15:34

标签: r dplyr

当我运行此命令时:  filter(pitches, stand == "L", des == "Ball" | des == "Called Strike")

它似乎无法正常工作(请注意展台列未经过滤),我明白了这一点:

     num                   gameday_link stand b_height     px    pz  type           des
   (dbl)                          (chr) (chr)    (chr)  (dbl) (dbl) (chr)         (chr)
1     21 gid_2016_04_09_houmlb_milmlb_1     R     5-11  0.710 2.256     S Called Strike
2     37 gid_2016_04_09_houmlb_milmlb_1     R      6-4  0.819 3.336     S Called Strike
3     33 gid_2016_04_14_milmlb_slnmlb_1     L      6-3 -0.458 4.464     B          Ball
4     33 gid_2016_04_14_milmlb_slnmlb_1     L      6-3 -0.805 2.818     S Called Strike
5     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.761 3.664     B          Ball
6     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.574 3.472     B          Ball
7     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.474 3.617     B          Ball
8     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.350 1.267     B          Ball
9     50 gid_2016_04_19_milmlb_minmlb_1     L      6-0 -1.610 2.499     B          Ball
10    50 gid_2016_04_19_milmlb_minmlb_1     L      6-0 -1.177 1.543     B          Ball

当我运行此命令时: filter(pitches, stand == "L", des == "Ball")

它有效,我明白了:

     num                   gameday_link stand b_height     px    pz  type   des
   (dbl)                          (chr) (chr)    (chr)  (dbl) (dbl) (chr) (chr)
1     33 gid_2016_04_14_milmlb_slnmlb_1     L      6-3 -0.458 4.464     B  Ball
2     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.761 3.664     B  Ball
3     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.574 3.472     B  Ball
4     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.474 3.617     B  Ball
5     17 gid_2016_04_19_milmlb_minmlb_1     L     5-10 -0.350 1.267     B  Ball
6     50 gid_2016_04_19_milmlb_minmlb_1     L      6-0 -1.610 2.499     B  Ball
7     50 gid_2016_04_19_milmlb_minmlb_1     L      6-0 -1.177 1.543     B  Ball
8     50 gid_2016_04_19_milmlb_minmlb_1     L      6-0 -1.072 1.091     B  Ball
9     50 gid_2016_04_19_milmlb_minmlb_1     L      6-0 -0.987 3.506     B  Ball
10    34 gid_2016_04_19_milmlb_minmlb_1     L     5-10  1.962 2.302     B  Ball

为什么会这样?

1 个答案:

答案 0 :(得分:0)

所以似乎dplyr的过滤器没有在逻辑参数周围添加括号。

所以我的命令

filter(pitches, stand == "L", des == "Ball" | des == "Called Strike")

成为

stand == "L" AND des == "Ball" OR des == "Called Strike"

正确的命令是

filter(pitches, stand == "L", (des == "Ball" | des == "Called Strike"))