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

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

问题描述

我正在尝试从循环写入 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 循环写入数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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