如何在R中创建一个包含两列和五行的简单表?我目前有以下循环:
for (i in 0:5) {
red <- sprintf("%d white balls and not the red ball: %f",i,win_balls(i, TRUE))
not_red <- sprintf("%d white balls and the red ball: %f",i,win_balls(i, FALSE))
print(not_red)
print(red)
}
我不想打印not_red
和red
,而是将它们分别存储在表格的相应列中。所以表格就像这样:
Not Red Red
0 2
1 3
4 5
答案 0 :(得分:1)
您可以使用lapply
将其创建为矩阵:
do.call(rbind, lapply(0:5, function(i) {
c(i, i*2)
}))
或t(sapply())
:
t(sapply(0:5, function(i) {
c(i, i*2)
}))
然后你只需要设置列名。
对于win_balls
函数,可能会有效
balls <- do.call(rbind, lapply(0:5, function(i) {
c(win_balls(i, TRUE), win_balls(i, FALSE))
}))
colnames(balls) <- c("NotRed", "Red")