table()
requires something that it can interpret as a factor, which can be in a data frame, but it can't be a data frame itself. What the filter
function pipes on to table is the subset of the data frame containing only the rows matching the filter
condition. While {dplyr}
functions receiving the pipe treat an incoming data frame as implicit and search the names
within it to interpret the argument sex
, table
makes so such effort.
One of the downsides of using tidy
-flavored R
is that it inculcates habits that come to expect that the rest of R
shares the same mindset. While that often doesn't present a problem, when it does it can be harder to detect the disconnects that cause it because of course the downstream pipe function knows about the variables contained in the data frame.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# mixed metaphor
iris |> filter(Sepal.Length > 5.5) |> select(Species) |> table()
#> Species
#> setosa versicolor virginica
#> 3 39 49
# tidyesque
iris |>
dplyr::filter(Sepal.Length > 5.5) |>
group_by(Species) |>
count()
#> # A tibble: 3 Ă— 2
#> # Groups: Species [3]
#> Species n
#> <fct> <int>
#> 1 setosa 3
#> 2 versicolor 39
#> 3 virginica 49
# base consise, when using bracket subseting,
# dealing with a handful of variables
iris[iris[1] > 5.5,5] |> table()
#>
#> setosa versicolor virginica
#> 3 39 49
# base wordy, when keeping positional track
# is harder due to the number of variables
iris[iris$Sepal.Length > 5.5,"Species"] |> table()
#>
#> setosa versicolor virginica
#> 3 39 49
Created on 2023-06-19 with reprex v2.0.2