R - Running a t-test from piping operators

假如想象 提交于 2019-12-30 03:25:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!