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?
%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).
The pipe operator, written as %>% , has been a longstanding feature of the magrittr package for R. It takes the output of one function and passes it into another function as an argument. This allows us to link a sequence of analysis steps.
We can place it inside summarise
as a list
d %>%
summarise(ttest = list(t.test(amount ~ group, var.equal = TRUE)))
and if we need to extract only the pvalue, this can be done
d %>%
summarise(pval = t.test(amount ~ group, var.equal = TRUE)$p.value)
Or we can place it inside the {}
and then do the t.test
d %>%
{t.test(.$amount ~ .$group, var.equal = TRUE)}
Or without the braces by specifying the data
for the formula method
d %>%
t.test(amount ~ group, data = ., var.equal = TRUE)
EDIT: based on @hpesoj626's comments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With