应用r中列表组件内的循环和r中绑定列表组件

应用r中列表组件内的循环和r中绑定列表组件,r,list,for-loop,bind,R,List,For Loop,Bind,下面是我遇到问题的示例数据和部分函数 # data Gr1 <- data.frame (group = rep(1:2, each = 2001), position = round (c(0, cumsum (rnorm (2000, 0.05, 0.08)), 0, cumsum (rnorm (2000, 0.05, 0.08))))) Gr2 <- data.frame (group = rep(1:2, each = 2001), position = rou

下面是我遇到问题的示例数据和部分函数

# data
Gr1 <- data.frame (group = rep(1:2, each = 2001), 
position = round (c(0, cumsum (rnorm (2000, 0.05, 0.08)), 0, 
cumsum (rnorm (2000, 0.05, 0.08)))))

 Gr2 <- data.frame (group = rep(1:2, each = 2001), 
 position = round (c(0, cumsum (rnorm (2000, 0.05, 0.08)), 0, 
  cumsum (rnorm (2000, 0.04, 0.08)))))

 Gr3 <- data.frame (group = rep(1:2, each = 2001), 
position = round (c(0, cumsum (rnorm (2000, 0.05, 0.08)), 0, 
 cumsum (rnorm (2000, 0.05, 0.08)))))

 groupobs = list(Gr1, Gr2, Gr3)
 grnames = c("A", "B", "C")
 spacing = c(0, 0.1, 0.3)


    # for loop 

    for (i in 1:length(groupobs)){
     groupobs[i]$sgrp <- grnames[i]
     groupobs[i]$y <-  groupobs[i]$group + spacing[i]
    }

    # binding of list components
    mydf <- data.frame (rbind (groupobs)
#数据

Gr1我可能离这里很远,但是

mydf<-mapply(function(a,b,c){a$sgrp<-b;a$y<-c;a},groupobs,grnames,spacing,SIMPLIFY = F)
mydf<-do.call("rbind",mydf)
> str(mydf)
'data.frame':   12006 obs. of  4 variables:
 $ group   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ position: num  0 0 0 0 0 0 0 0 0 1 ...
 $ sgrp    : chr  "A" "A" "A" "A" ...
 $ y       : num  0 0 0 0 0 0 0 0 0 0 ...

mydf@ttmaccer谢谢你的评论,请看我的编辑,希望我已经解释清楚了我想要实现什么,并避免在这个问题上投反对票!我是ttmaccer的,请注意,请改进您的问题是我认为最好的方式…不花时间考虑我的意见的向下投票和关闭投票似乎对答案太苛刻了,我希望数据帧绑定到一个。然而,上面的功能很好地创建了一个列表列表。可能是您想要做的。通过mydf调用(“rbind”,mydf)
mydf<-mapply(function(a,b,c){a$sgrp<-b;a$y<-c;a},groupobs,grnames,spacing,SIMPLIFY = F)
mydf<-do.call("rbind",mydf)
> str(mydf)
'data.frame':   12006 obs. of  4 variables:
 $ group   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ position: num  0 0 0 0 0 0 0 0 0 1 ...
 $ sgrp    : chr  "A" "A" "A" "A" ...
 $ y       : num  0 0 0 0 0 0 0 0 0 0 ...