R-从管道运营商处进行t检验 [英] R - Running a t-test from piping operators

查看:141
本文介绍了R-从管道运营商处进行t检验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从piping运算符运行t.test?
我试图找到答案,但是围绕该主题的大多数问题都着眼于在同一数据集上进行许多测试.
我看过一个broom程序包,但这似乎对读取结果很有帮助.
我感兴趣的是是否可以仅使用piping并在输出上运行t.test().
例如,这是一些示例数据

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)
  )

如果我使用base R运行t.test,则会得到结果:

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

但是,如果我尝试使用piping,则会收到错误消息:

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?

推荐答案

我们可以将其作为list

d %>%
  summarise(ttest = list(t.test(amount ~ group, var.equal = TRUE))) 

如果只需要提取pvalue,就可以做到

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)


或者我们可以将其放置在{}内,然后执行t.test


Or we can place it inside the {} and then do the t.test

d %>%
     {t.test(.$amount ~ .$group, var.equal = TRUE)}

或者通过为公式方法指定data而不使用大括号

Or without the braces by specifying the data for the formula method

d %>%
     t.test(amount ~ group, data = ., var.equal = TRUE)

基于@ hpesoj626的评论

based on @hpesoj626's comments

这篇关于R-从管道运营商处进行t检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆