outer() equivalent for non-vector lists in R

后端 未结 3 1132
孤独总比滥情好
孤独总比滥情好 2020-12-31 15:38

I understand how outer() works in R:

> outer(c(1,2,4),c(8,16,32), \"*\")

     [,1] [,2] [,3]
[1,]    8   16   32
[2,]   16   32   64
[3,]   32   64  128
         


        
3条回答
  •  天涯浪人
    2020-12-31 16:01

    Although this is an old question, here is another solution that is more in the spirit of the outer function. The idea is to apply outer along the indices of list1 and list2:

    cor2 <- Vectorize(function(x,y) {
       vec1 <- list1[[x]]
       vec2 <- list2[[y]]
       cor(vec1,vec2,method="spearman")
    })
    outer(1:length(list1), 1:length(list2), cor2)
    

提交回复
热议问题