想通過簡化代碼來問這個問題(邏輯有點不可思議 - 但它與我的情況很相似),我正在使用的當前代碼很長,可能太多沒有價值的詞。我會很樂意添加什麼需要回答這個問題:將列表中的循環輸出轉換爲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
沒有辦法做到這一點沒有拋出自己掉下懸崖?