R:对 2 个数据帧的行进行 t 测试 [英] R: t tests on rows of 2 dataframes

查看:15
本文介绍了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天全站免登陆