R:在2个数据帧的行上进行t检验 [英] R: t tests on rows of 2 dataframes

查看:192
本文介绍了R:在2个数据帧的行上进行t检验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据帧,我想对行进行独立的2组t检验(即t.test(y1, y2),其中y1是dataframe1中的行,而y2是dataframe2中的匹配行)

I have two dataframes and I would like to do independent 2-group t-tests on the rows (i.e. t.test(y1, y2) where y1 is a row in dataframe1 and y2 is matching row in dataframe2)

实现此目标的最佳方法是什么?

whats best way of accomplishing this?

我刚刚找到了格式:dataframe1 [i,] dataframe2 [i,].这将循环工作.那是最好的解决方案吗?

I just found the format: dataframe1[i,] dataframe2[i,]. This will work in a loop. Is that the best solution?

推荐答案

您概述的方法是合理的,只需确保预先分配存储向量即可.我会仔细检查您是否真的要比较行而不是列.我使用的大多数数据集都将每一行作为观察单位,而各列代表感兴趣的单独响应/列,无论如何,它都是您的数据-因此,如果您需要这样做,可以采用以下方法:

The approach you outlined is reasonable, just make sure to preallocate your storage vector. I'd double check that you really want to compare the rows instead of the columns. Most datasets I work with have each row as a unit of observation and the columns represent separate responses/columns of interest Regardless, it's your data - so if that's what you need to do, here's an approach:

#Fake data
df1 <- data.frame(matrix(runif(100),10))
df2 <- data.frame(matrix(runif(100),10))


#Preallocate results
testresults <- vector("list", nrow(df1))
#For loop
for (j in seq(nrow(df1))){
  testresults[[j]] <- t.test(df1[j,], df2[j,])
}

现在,您有一个列表,只要该列表在df1中具有行即可.然后,我建议使用lapplysapply轻松地从列表对象中提取内容.

You now have a list that is as long as you have rows in df1. I would then recommend using lapply and sapply to easily extract things out of the list object.

这篇关于R:在2个数据帧的行上进行t检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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