错误:要替换的项目数不是替换长度的倍数 - R

问题描述 投票:0回答:0

我正在尝试遍历三维数组以将输出保存在二维矩阵中,但我不断收到以下错误

Error in results[i, j] <- purrr::map_dbl(quantile(res[i, j, ], probs = c(0.025, : number of items to replace is not a multiple of replacement length
.

我认为我已经正确定义了矩阵的维度,首先打印输出以检查维度。下面是示例数据和我的脚本。

#the data
res <- structure(c(15.5782878399256, 1.96934035595117, 0.200343692642543, 
                   0.871510655485173, 17.2682837511044, 0.66041093390068, -0.511567198342732, 
                   1.29802802862954, 14.8385457149254, 2.13218438628733, 0.562995626888043, 
                   0.590605706350054, 17.7278518630607, 0.969587340270382, -0.244104144510527, 
                   0.81626749897487, 13.6035736799169, 2.8514028662032, 0.245060828129327, 
                   0.769277990479192, 17.861084907096, 1.24191875216756, -0.682692716469404, 
                   0.979774642660928, 16.1742729291693, 2.2689054962442, 0.368872835662668, 
                   0.213605569846145, 17.5504922976792, 0.607365171022073, -0.312174464330811, 
                   0.918963681182121, 14.1982438110978, 2.14689175512824, 0.205770576226154, 
                   1.08915659087064, 16.6044760172988, 0.91587119402875, -0.477062203063226, 
                   1.31722296414953), dim = c(4L, 2L, 5L), 
                   dimnames = list(c("tint", "psy", "ext", "new"), c("ct", "ci"), NULL))

#the variables
Xq <- c('tint', 'psy', 'ext', 'new')
Yq <- c('ct', 'ci')

#the script
results <- matrix(nrow=(length(Xq) * length(Yq)), ncol=length(Yq))
for (i in seq_along(Xq)){
  for (j in seq_along(Yq)){
    print(purrr::map_dbl(quantile(res[i, j, ], probs = c(0.025, 0.975)), purrr::pluck))
    results[i, j] = purrr::map_dbl(quantile(res[i, j, ], probs = c(0.025, 0.975)), purrr::pluck) # where the error lies
  }  
}
quantile(res["tint", "ct", ], probs = c(0.025, 0.975))  # test output
r for-loop matrix multidimensional-array
© www.soinside.com 2019 - 2024. All rights reserved.