0

Amateur user of R and just having difficulties with simplifying some code. I have a list of dataframes of test results and I would like run the sjt.corr function across these dataframes for quality assurance.

Edit

A small reproducible example:

library(sjplot)
list <- list()
list$a <- as.data.frame(cbind(c(1,2,3,4,5,6,7,8),c(1,2,3,4,5,7,6,8)))
list$b <- as.data.frame(cbind(c(1,2,3,4,5,7,6,8),c(7,6,8,5,4,3,2,1)))
list$c <- as.data.frame(cbind(c(7,6,8,5,4,3,2,1),c(1,2,3,4,5,6,7,8)))

I am not sure where my loop fails me. If you can see the error please let me know:

for (i in seq_along(list)) {
  sjt.corr(
    list[[i]],
    na.deletion = "pairwise",
    corr.method = "spearman",
    file = paste("consensus", i, "html", sep = ".")
  )
} 

Appears to work silently, but does not save anything.

This works:

sjt.corr(
  list[[1]],
  na.deletion = "pairwise",
  corr.method = "spearman",
  file = paste("consensus", 1, "html", sep = ".")
)
4

1 回答 1

1

So a solution from my friend is to just use lapply.

i <- 0

lapply(list, function(x) {
  i <<- i + 1
  sjt.corr(
    x,
    na.deletion = "pairwise",
    corr.method = "spearman",
    file = paste("consensus", i, "html", sep = "."))
})

Which works great! Still unsure why the original for loop does not work. Any reference to another similar scenario would be great.

于 2017-11-16T02:46:50.587 回答