Is it possible to run a t.test from piping operators?
I tried to find the answer to this, but most of the questions around this topic look at doing many tests on the same dataset.
I've looked a broom package, but it seems to be good for reading the results.
What I'm interested in is whether it's possible to just use piping and run the t.test() on the output.
For example, here is some sample data
library(dplyr)
d <- data.frame(
group = sample(LETTERS[1:2], size = 10, replace = T),
amount = sample(1:3, size = 10, replace = T)
)
If I run a t.test using base R, I get the results:
t.test(d$amount~d$group, var.equal = T)
> d
group amount
1 A 2
2 A 2
3 B 1
4 B 3
5 A 2
6 B 1
7 B 2
8 A 1
9 B 3
10 A 3
But if I try an use piping, I get errors:
d %>% t.test(amount~group, var.equal = T)
Error: is.atomic(x) is not TRUE
In addition: Warning messages:
1: In is.na(y) :
is.na() applied to non-(list or vector) of type 'language'
2: In mean.default(x) : argument is not numeric or logical: returning NA
3: In var(x) : NAs introduced by coercion
4: In mean.default(y) : argument is not numeric or logical: returning NA
Do I need to do some additional manupulations?
We can place it inside
summariseas alistand if we need to extract only the pvalue, this can be done
Or we can place it inside the
{}and then do thet.testOr without the braces by specifying the
datafor the formula methodEDIT: based on @hpesoj626's comments