将多个列出的数据帧循环到一个函数中时的加权平均错误

时间:2018-12-13 17:15:16

标签: r list lapply mclapply

我昨天在下面的链接中发布了问题,Emil Bode给了很好的答案! Looping multiple listed data frames into a single function

但是,我忘记转换一个样本列表,因此在尝试对转换后的数据运行时抛出错误。

    #######################################
          library(parallel)
          library(ade4)
        # testing functions ... 
        d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6))
        d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4))
        d3 <- data.frame(y1 = c(2, 1, 2), y2 = c(5, 6, 4))
        spec.list <- list(d1, d2, d3)

        d1 <- data.frame(y1 = c(20, 87, 39,7,8,1,1), y2 = c(46, 51, 8,20, 87, 39,0))
        d2 <- data.frame(y1 = c(30, 21, 12,1,0,0,2), y2 = c(61, 51, 33,30, 21, 12,21))
        d3 <- data.frame(y1 = c(2, 11, 14,10,2,3,5), y2 = c(52, 16, 1,2, 11, 14,66))
        env.list <- list(d1, d2, d3)

        d1 <- data.frame(y1 = c(0.15, 0.1, 0.9,30, 21), y2 = c(0.46, 0.51, 0.82,30,1))
        d2 <- data.frame(y1 = c(0.13, 0.31, 0.9,2, 11), y2 = c(0.11, 0.51, 0.38,1,0.1))
        d3 <- data.frame(y1 = c(0.52, 0.11, 0.14,1,5), y2 = c(0.52, 0.36, 0.11,0.7,2))
        spat.list <- list(d1, d2, d3)


        spec.dudi <- mclapply(spec.list, function(x){
          dudi.pca(x, scale = F, scannf = F)
        }) # had to transform these data, like this. 

        output_varpart <- mclapply(1:3, function(x){
          varipart(spec.dudi[[x]], env.list[[x]], spat.list[[x]], type = "parametric")
        }) # the [mc]lapply function results in failure ( Error in weighted.mean.default(newX[, i], ...) : 
  'x' and 'w' must have the same length). 

    # I also checked and the lengths are all the same!! 
    > length(spec.dudi)
    [1] 3
    > length(env.list)
    [1] 3
    > length(spat.list)
    [1] 3

这适用于单个数据帧,所以我不确定为什么会这样!与清单有关吗?无论哪种方式,我该如何解决?

0 个答案:

没有答案