Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

Why lapply works and apply doesn't?

$
0
0

My data:

 df_1 <- data.frame(
      x = replicate(
        n = 3, 
        expr = runif(n = 30, min = 20, max = 100)
      ), 
      y = sample(
        x = 1:3, size = 30, replace = TRUE
      )
    )

The follow code with lapply works:

lapply(X = names(df_1)[c(1:3)], FUN = function(x) {
  pairwise.t.test(
    x = df_1[, x], 
    g = df_1[['y']], 
    p.adj = 'bonferroni'
  )
})

But, with apply doesn't:

apply(X = names(df_1)[c(1:3)], MARGIN = 2, FUN = function(x) {
  pairwise.t.test(
    x = df_1[, x], 
    g = df_1[['y']], 
    p.adj = 'bonferroni'
  )
})

Error in apply(X = names(df_1)[c(1:3)], MARGIN = 2, FUN = function(x) { : dim(X) must have a positive length

Why the problem? Are they not equivalent?


Viewing all articles
Browse latest Browse all 201977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>