将循环输出存储在R中的数据帧中 [英] Storing loop output in a dataframe in R

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

问题描述

我想知道如何将完整循环输出的值存储到R中的单个数据帧中.例如,

I want to know how to store the values of the complete loop output into a single dataframe in R. For example,

for(i in unique(x$id)){
    .
    .
    .
    y=output of one iteration}

在每次迭代结束时,我将得到y的输出.但是我想将所有迭代的输出存储到y中.如何在R中做到这一点?

At the end of each iteration, I am getting the output in y. But I want to store output of all iterations into y. How do I do that in R?

推荐答案

您可以以y作为空的data.frame开头,如下所示:y <- data.frame().然后在每次迭代结束时将行绑定到此data.frame,如:y <- rbind.data.frame(y, [output of one interation]).但是您也可以通过将其包装在lapplydo.call中来使其更加紧密,如下所示:

You can begin with y as an empty data.frame as in: y <- data.frame(). Then bind the rows to this data.frame at the end of each iteration as in: y <- rbind.data.frame(y, [output of one interation]). But you can also make this a little more tight by wrapping it in an lapply and do.call as in:

y <- do.call(rbind.data.frame,
             lapply(unique(x$id),
                    function(i){
       ...;
       return([output of one iteration])}))

这篇关于将循环输出存储在R中的数据帧中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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