使用'和'的dplyr滤波器逻辑和'或'

时间:2018-03-30 19:44:13

标签: r dplyr

我希望mtcars数据集的两个要点都是 TRUE

  • cyl > 4和 mpg > 10
  • vs > 0或 carb > 1

'适当的'用dplyr写这个的方法?是否有某种可视化解释和/或? "和"之间的区别"和,或,"以及那种东西。我想我在下面说得对,但这总是让我感到困惑。我认为R inferno中有一章叫做#34;和andand"讽刺这种情况。

library(dplyr)
mtcars %>% 
  filter(
    cyl > 4,
    mpg > 10,
    vs > 0 | 
      carb > 1
  )

1 个答案:

答案 0 :(得分:3)

你的表达没问题 - 据我所知,你可以, filter(...)&中的每个mtcars %>% filter(cyl > 4) %>% nrow() # 21 mtcars %>% filter(mpg > 20) %>% nrow() # 14 mtcars %>% filter(cyl > 4, mpg > 20) %>% nrow() # 3 mtcars %>% filter(cyl > 4 & mpg > 20) %>% nrow() # 3 identical(mtcars %>% filter(cyl > 4, mpg > 20), mtcars %>% filter(cyl > 4 & mpg > 20)) # TRUE mtcars %>% filter(cyl > 4, mpg > 20, vs > 0) %>% nrow() # 1 mtcars %>% filter(cyl > 4, mpg > 20, carb > 1) %>% nrow() # 2 mtcars %>% filter(cyl > 4, mpg > 20, vs > 0 | carb > 1) %>% nrow() # 3 identical(mtcars %>% filter(cyl > 4 & mpg > 20 & (vs > 0 | carb > 1)), mtcars %>% filter(cyl > 4, mpg > 20, vs > 0 | carb > 1)) # TRUE 进行处理:

Sub combine_all_Reports()

Dim J As Integer
Dim s As Worksheet

On Error Resume Next

For Each s In ActiveWorkbook.Sheets
    If s.Name <> "Combined Reports" Then
        Application.GoTo Sheets(s.Name).[B9]
        Selection.CurrentRegion.Select
        Selection.Copy Destination:=Sheets("Combined Reports"). _
        Cells(Rows.Count, 1).End(xlUp)(2)
        wksCombinedReports.Cells.EntireColumn.AutoFit
    End If
Next

End Sub