问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/50036411/r-running-a-t-test-from-piping-operators