# [R] looping function through list

arun smartpink111 at yahoo.com
Thu Jan 2 23:14:23 CET 2014

```HI,
I tested it on R 3.0.2 console (linux) and also on Rstudio Version 0.98.490.  It seems alright.
A.K.

Thanks for this. I am trying to run the code you posted but Rstudio keeps crashing. I am trying to run it on the example output1 since it's small but that crashes as well.

On Thursday, January 2, 2014 2:36 PM, arun <smartpink111 at yahoo.com> wrote:

#or
mapply(emd2d,sapply(output1,`[`,1),sapply(output1,`[`,2))
#[1]       NaN -6.089909
A.K.

On Thursday, January 2, 2014 2:33 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
May be this helps:
set.seed(42)
output1 <- list(list(matrix(0,8,11),matrix(0,8,11)), list(matrix(rnorm(80),8,10),matrix(rnorm(80),8,10)))
library(emdist)
sapply(output1,function(x) {emd2d(x[[seq_along(x)[1]]],x[[seq_along(x)[2]]]) })
#[1]       NaN -6.089909

A.K.

I'm trying to apply a function to a list using rapply but I'm having
trouble doing so. I'm trying to calculate the earth-movers distance
using the emdist package. Every index in the list has two subindices. I
want to calculate the earth-movers distance for these subindices
iteratively. An example of the list:

[[1]]
[[1]][[1]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,]    0    0    0    0    0    0    0    0    0     0     0
[2,]    0    0    0    0    0    0    0    0    0     0     0
[3,]    0    0    0    0    0    0    0    0    0     0     0
[4,]    0    0    0    0    0    0    0    0    0     0     0
[5,]    0    0    0    0    0    0    0    0    0     0     0
[6,]    0    0    0    0    0    0    0    0    0     0     0
[7,]    0    0    0    0    0    0    0    0    0     0     0
[8,]    0    0    0    0    0    0    0    0    0     0     0

[[1]][[2]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,]    0    0    0    0    0    0    0    0    0     0     0
[2,]    0    0    0    0    0    0    0    0    0     0     0
[3,]    0    0    0    0    0    0    0    0    0     0     0
[4,]    0    0    0    0    0    0    0    0    0     0     0
[5,]    0    0    0    0    0    0    0    0    0     0     0
[6,]    0    0    0    0    0    0    0    0    0     0     0
[7,]    0    0    0    0    0    0    0    0    0     0     0
[8,]    0    0    0    0    0    0    0    0    0     0     0

[[2]]
[[2]][[1]]
[,1]     [,2]     [,3]     [,4]     [,5]    [,6]     [,7]     [,8]     [,9]    [,10]
[1,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.549675 5.834462 5.401988 5.933774
[2,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.304306 5.834462 5.401988 5.933774
[3,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.485151 5.834462 5.401988 5.933774
[4,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.691836 5.834462 5.401988 5.933774
[5,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.691836 5.834462 5.401988 5.933774
[6,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.691836 5.834462 5.401988 5.933774
[7,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.691836 5.834462 5.401988 5.933774
[8,] 1.336731 3.264521 4.567414 4.871417 4.386032 5.75678 6.691836 5.834462 5.401988 5.933774
[,11]
[1,] 6.549675
[2,] 6.304306
[3,] 6.485151
[4,] 6.790983
[5,] 7.102360
[6,] 7.211278
[7,] 7.211278
[8,] 7.164059

[[2]][[2]]
[,1]     [,2]     [,3]     [,4]      [,5]     [,6]     [,7]     [,8]     [,9]
[1,] 6.886406 8.814196 10.11709 10.42109  9.935707 11.30645 12.24151 11.38414 10.95166
[2,] 6.641038 8.568828  9.87172 10.17572  9.690339 11.06109 11.99614 11.13877 10.70629
[3,] 6.821883 8.749673 10.05257 10.35657  9.871184 11.24193 12.17699 11.31961 10.88714
[4,] 7.127715 9.055504 10.35840 10.66240 10.177015 11.54776 12.48282 11.62545 11.19297
[5,] 7.439092 9.366881 10.66977 10.97378 10.488392 11.85914 12.79420 11.93682 11.50435
[6,] 7.749465 9.677255 10.98015 11.28415 10.798766 12.16951 13.10457 12.24720 11.81472
[7,] 7.783697 9.711487 11.01438 11.31838 10.832998 12.20375 13.13880 12.28143 11.84895
[8,] 7.500790 9.428580 10.73147 11.03548 10.550091 11.92084 12.85590 11.99852 11.56605
[,10]    [,11]
[1,] 11.48345 12.76095
[2,] 11.23808 12.51558
[3,] 11.41893 12.69643
[4,] 11.72476 13.00226
[5,] 12.03613 13.31364
[6,] 12.34651 13.62401
[7,] 12.38074 13.65824
[8,] 12.09783 13.37534

I have tried combining rapply and do.call in this fashion but it has failed so far:

library(emdist)
do.call(rbind, rapply(output, function(x,y) emd2d))

The error message I get is:
Error in (function (..., deparse.level = 1)  :
cannot coerce type 'closure' to vector of type 'list'

Any ideas?

```