Apply function taking matching columns in 2 data frames, looping over columns

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top