-1

I have a for loop containing the following:

for (i in 1:100) {

  #calculate correlation
  correlationList1a[[i]] <- sapply(seq(1,14), 
                    function(x) cor(validationSetsA.list[[i]][,x], medianListA[[i]]))
  correlationList2a[[i]] <- sapply(seq(1,14), 
                    function(x) cor(validationSetsA.list[[i]][,x], medianListB[[i]]))

}

How can I simplify this? correlationList1a and correlationList2a are basically doing the same thing the only thing that is different is that correlation1a contains medianListA and correlationList2a contains medianListB.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
kimmie
  • 225
  • 1
  • 2
  • 5
  • 1
    Please provide a **minimal, self contained example**. Check these links for general ideas, and how to do it in R: [**here**](http://stackoverflow.com/help/mcve), [**here**](http://www.sscce.org/), [**here**](http://adv-r.had.co.nz/Reproducibility.html), and [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – Henrik Oct 24 '14 at 11:40
  • Sounds like a job for `mapply` maybe? – Roman Luštrik Oct 24 '14 at 11:43

1 Answers1

1

It looks like this is a case for mapply.

mapply(function(x, y) apply(x[,seq(1,14)], 2, cor, y=y), 
       x = validationSetsA.list, 
       y = medianListA, 
       SIMPLIFY = FALSE)
shadow
  • 21,823
  • 4
  • 63
  • 77