Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: t tests on rows of 2 dataframes

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?

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

like image 335
bdeonovic Avatar asked Feb 19 '23 02:02

bdeonovic


1 Answers

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,])
}

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.

like image 186
Chase Avatar answered Feb 22 '23 00:02

Chase