想通过简化代码来问这个问题(逻辑有点不可思议 - 但它与我的情况很相似),我正在使用的当前代码很长,可能太多没有价值的词。我会很乐意添加什么需要回答这个问题:将列表中的循环输出转换为R中的数据框
我有一个情况与for循环,例如:
data2 <- data.frame("Chocolate" = c(0.25), "Strawberry" = c(.16),
"Vanilla" = c(0.64), "Blueberry" = c(.75))
for (i in 1:4) {
freqSim <- data.frame(sample(0:1, length(1:100), replace=T, prob = c(1-data2[i],data2[i])))
lossCol <- freqSim*(runif(n=100, min=0, max=7000))
costAvg <- mean(as.numeric(unlist(lossCol)))
costSD <- sd(as.numeric(unlist(lossCol)))
costAvg <- formatC(costAvg, format='d', big.mark=",")
costSD <- formatC(costSD, format='d', big.mark= ",")
stats <- list()
stats[[i]] <- list(costAvg,costSD)
print(stats[[i]])
}
在那里我得到返回的载体,诸如:
[[1]]
[1] "1,261"
[[2]]
[1] "2,103"
[[1]]
[1] "313"
[[2]]
[1] "1,165"
[[1]]
[1] "2,073"
[[2]]
[1] "2,206"
[[1]]
[1] "2,417"
[[2]]
[1] "2,258"
我会理想像,看起来像一个矩阵:
Chocolate Strawberry Vanilla Blueberry
Label A 1,261 313 2,073 2,417
Label B 2,103 1,165 2,206 2,258
没有办法做到这一点没有抛出自己掉下悬崖?