分组变量的T检验 [英] T-test with grouping variable

查看:254
本文介绍了分组变量的T检验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含36个变量和74个观察值的数据框.我想用35个变量和1个分组变量(两个级别)对两个样本进行ttest配对.

I've got a data frame with 36 variables and 74 observations. I'd like to make a two sample paired ttest of 35 variables by 1 grouping variable (with two levels).

例如:数据框包含年龄",体重"和组"变量. 现在,我想可以使用以下代码对每个变量进行ttest:

For example: the data frame contains "age" "body weight" and "group" variables. Now I suppose I can do the ttest for each variable with this code:

t.test(age~group)

但是,有一种方法可以用一个代码而不是一个一个地测试所有35个变量吗?

But, is there a way to test all the 35 variables with one code, and not one by one?

推荐答案

示例数据框:

dat <- data.frame(age = rnorm(10, 30), body = rnorm(10, 30), 
                  weight = rnorm(10, 30), group = gl(2,5))

您可以使用lapply:

lapply(dat[1:3], function(x) 
                   t.test(x ~ dat$group, paired = TRUE, na.action = na.pass))

在上面的命令中,1:3表示包含变量的列数.参数paired = TRUE是执行配对t检验所必需的.

In the command above, 1:3 represents the numbers of the columns including the variables. The argument paired = TRUE is necessary to perform a paired t-test.

这篇关于分组变量的T检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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