1

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!

wy6622
  • 13
  • 3

2 Answers2

1

apply, lapply, vapply and sapply all loop over a single object. If you've got multiple, 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.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
0

You can use a regular for loop in R to achieve this by looping over the column names.

cols <- c("x1", "x2", "x3", "x4", "x5")
df1 <- as.data.frame(matrix(runif(50), nrow = 10, byrow = TRUE))
colnames(df1) <- cols
df2 <- as.data.frame(matrix(runif(100), nrow = 20, byrow = TRUE))
colnames(df2) <- cols

for (col in cols) {
  message(paste("Testing column", col, collapse = " "))
  print(paste("t-statistic: ", t.test(df1[col], df2[col])$statistic[["t"]]))
  print(paste("p-value:     ", t.test(df1[col], df2[col])$p.value))
}
#> Testing column x1
#> [1] "t-statistic:  0.419581290015361"
#> [1] "p-value:      0.68029340912263"
#> Testing column x2
#> [1] "t-statistic:  -0.343435717107623"
#> [1] "p-value:      0.7361266387073"
#> Testing column x3
#> [1] "t-statistic:  0.248037735890824"
#> [1] "p-value:      0.807107717907307"
#> Testing column x4
#> [1] "t-statistic:  0.992363174130968"
#> [1] "p-value:      0.333989277352541"
#> Testing column x5
#> [1] "t-statistic:  2.06600413500528"
#> [1] "p-value:      0.0527652252424411"

Created on 2020-11-02 by the reprex package (v0.3.0)

Eric Leung
  • 2,354
  • 12
  • 25