从R中的for-loop写入数据帧 [英] writing to a dataframe from a for-loop in R

查看:152
本文介绍了从R中的for-loop写入数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个循环写入到R中的数据框,例如这样一个循环。

I'm trying to write from a loop to a data frame in R, for example a loop like this>

for (i in 1:20) {
print(c(i+i,i*i,i/1))}

,并将每行3个值写入具有三列的数据框,以便每次迭代都占用一个新行。我试过使用矩阵,ncol = 3并以行填充,但只能从循环中获取最后一个项。

and to write each line of 3 values to a data frame with three columns, so that each iteration takes on a new row. I've tried using matrix, with ncol=3 and filled by rows, but only get the last item from the loop.

谢谢。

推荐答案

您可以使用rbind:

You could use rbind:

d <- data.frame()
for (i in 1:20) {d <- rbind(d,c(i+i, i*i, i/1))}

这篇关于从R中的for-loop写入数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆