So this is really a basic question, however I am stuck with trying to use the following basic code:
dc_test |> table(testVar)
It leads to:
Error: Object 'testVar' not found
dc_test is a data.frame. The column testVar is "character" class.
It works without an error if i directly use the table() function, however this is not as handy for including filtering and other operations:
table(dc_test$testVar)
-> I get the table without an error.
Does the table() function not work with piping or am I using it wrong?
I tried
dc_test |> table(testVar)
dc_test |> table(data = _$testVar)
dc_test |> table(testVar)
is exactly the same as table(dc_test, testVar)
, and if you run that line you will get the same exact error.
The pipe doesn't let you use column names without $
. Many dplyr
functions do let you do that, and they are often used with pipes, but you can use pipes without dplyr
and you can use dplyr
without pipes. They're unrelated, just often combined.
What you can do is pipe into the with()
function. The whole point of the with()
function is to let you use column names without quotes or $
. So this should work:
dc_test |> with(table(testVar))
## exact equivalent to
with(dc_test, table(testVar))
Or you could use count
, the dplyr
version of table
:
library(dplyr)
dc_test |> count(testVar)