I have the following two data frames
df1 <- as.data.frame(matrix(runif(50), nrow = 10, byrow = TRUE))
colnames(df1) <- c("x1", "x2", "x3", "x4", "x5")
df2 <- as.data.frame(matrix(runif(100), nrow = 20, byrow = TRUE))
colnames(df2) <- c("x1", "x2", "x3", "x4", "x5")
And I would like to test if the mean of columns x_j is the same for the 2 dfs, for j=1,...,5, recording the test statistic and p value.
t.test(df1$x1, df2$x1)$statistic
t.test(df1$x1, df2$x1)$p.value
apply() seems to only take one df as input? What's the best way to loop the above 2 lines over j?
Thanks in advance!
apply
, lapply
, vapply
and sapply
all loop over a single object. If you've got m
ultiple, you want mapply
or Map
:
mapply(function(x,y) t.test(x,y)[c("statistic","p.value")], df1, df2)
# x1 x2 x3 x4 x5
#statistic 0.6816886 -1.408304 -0.2598513 -0.890468 -1.097354
#p.value 0.5028386 0.1721202 0.7982655 0.3825847 0.2851621
This assumes both df1
and df2
are in the same column order.