Storing loop output in a dataframe in R

前端 未结 2 1035
旧巷少年郎
旧巷少年郎 2021-02-06 17:54

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=out         


        
相关标签:
2条回答
  • 2021-02-06 18:16

    You can do this simply by

    y  <- NULL;
    for (i in unique(x$id))
     { 
      tmp <- [output of one iteration]
      y <- rbind(y, tmp)
     }
    
    0 讨论(0)
  • 2021-02-06 18:23

    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])}))
    
    0 讨论(0)
提交回复
热议问题