R中的简单表格

时间:2015-09-13 02:56:49

标签: r

如何在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_redred,而是将它们分别存储在表格的相应列中。所以表格就像这样:

Not Red  Red
0        2
1        3
4        5

1 个答案:

答案 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")