R - Running a t-test from piping operators
Asked Answered
A

2

9

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?

Adminicle answered 26/4, 2018 at 6:42 Comment(0)
L
21

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

Lazor answered 26/4, 2018 at 6:44 Comment(2)
@hpesoj626 You are right. I was checking the .$amount ~ .Lazor
great, all I was missing the the data = . argument.Adminicle
A
1

The 'rstatix' a package deals exactly with performing basic statistical tests within a pipe

https://cran.r-project.org/web/packages/rstatix/rstatix.pdf

rstatix provides wrappers of functions like t.test(), or cohensD(), making it possible to use them with pipe operators. Performing a t-test as you required would simply work using their wrapper of t.test(), t_test():

d %>% t_test(amount ~ group, var.equal = T)
Aphelion answered 17/4, 2021 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.