rbind 在 R 中的嵌套 for 循环中
rbind inside nested for loops in R
我需要创建不同的数据帧,在 for 循环中使用 rbind 存储不同的数据帧,每个 i 一个。
举个例子:
library(lubridate)
DATA <- data.frame("ID"=c("01","01","02","02","03","03","03","04","04","04","05","05","05","06","06","06"),"x"=c("2009","2012","2013","2009","2012","2011","2013","2009","2010","2010","2011","2010","2009","2010","2011","2013"),"y"=c("a","a","a","b","c","a","a","c","b","b","c","a","c","a","b","c"))
one <- c("a","b","c")
two <- c("2009","2010","2011","2012")
listofdfs <- list()
for (i in one) {
for (j in 1:(length(two)-1)) {
B <- NULL
A <- DATA[DATA$x %in% c(two[j],two[j+1]) & DATA$y==i ,]
# keep the oldest date
tmp<-as.Date(A$x,"%Y-%m-%d")
if (length(tmp)>0)
{
A$tag <- 0
names(tmp)<-1:nrow(A)
id_x <- as.numeric(tapply(tmp[year(tmp)>two[j]],A$ID[year(tmp)>two[j]],function(x){
id <- which.min(x)
names(x)[id]
}))
A$tag[id_x] <- 1
}
# aggregate datasets over different years
B <- rbind(B,A)
}
listofdfs[[i]] <- B
}
几乎是我想得到的,除了B不是不同A的聚合(每次都会覆盖A)。
我得到一个这样的列表:
$a
ID x y tag
2 01 2012 a 0
6 03 2011 a 0
$b
ID x y tag
15 06 2011 b 0
$c
ID x y tag
5 03 2012 c 0
11 05 2011 c 0
由于覆盖(我不想要),2010 不存在。
有什么想法吗?谢谢!
你写的地方
for (i in one) {
for (j in 1:(length(two)-1)) {
B <- NULL
应该是
for (i in one) {
B <- NULL
for (j in 1:(length(two)-1)) {
有输出
> listofdfs
$a
ID x y tag
1 01 2009 a 0
12 05 2010 a 0
14 06 2010 a 0
6 03 2011 a 0
121 05 2010 a 0
141 06 2010 a 0
2 01 2012 a 0
61 03 2011 a 0
$b
ID x y tag
4 02 2009 b 0
9 04 2010 b 0
10 04 2010 b 0
91 04 2010 b 0
101 04 2010 b 0
15 06 2011 b 0
151 06 2011 b 0
$c
ID x y tag
8 04 2009 c 0
13 05 2009 c 0
11 05 2011 c 0
5 03 2012 c 0
111 05 2011 c 0
我需要创建不同的数据帧,在 for 循环中使用 rbind 存储不同的数据帧,每个 i 一个。
举个例子:
library(lubridate)
DATA <- data.frame("ID"=c("01","01","02","02","03","03","03","04","04","04","05","05","05","06","06","06"),"x"=c("2009","2012","2013","2009","2012","2011","2013","2009","2010","2010","2011","2010","2009","2010","2011","2013"),"y"=c("a","a","a","b","c","a","a","c","b","b","c","a","c","a","b","c"))
one <- c("a","b","c")
two <- c("2009","2010","2011","2012")
listofdfs <- list()
for (i in one) {
for (j in 1:(length(two)-1)) {
B <- NULL
A <- DATA[DATA$x %in% c(two[j],two[j+1]) & DATA$y==i ,]
# keep the oldest date
tmp<-as.Date(A$x,"%Y-%m-%d")
if (length(tmp)>0)
{
A$tag <- 0
names(tmp)<-1:nrow(A)
id_x <- as.numeric(tapply(tmp[year(tmp)>two[j]],A$ID[year(tmp)>two[j]],function(x){
id <- which.min(x)
names(x)[id]
}))
A$tag[id_x] <- 1
}
# aggregate datasets over different years
B <- rbind(B,A)
}
listofdfs[[i]] <- B
}
几乎是我想得到的,除了B不是不同A的聚合(每次都会覆盖A)。
我得到一个这样的列表:
$a
ID x y tag
2 01 2012 a 0
6 03 2011 a 0
$b
ID x y tag
15 06 2011 b 0
$c
ID x y tag
5 03 2012 c 0
11 05 2011 c 0
由于覆盖(我不想要),2010 不存在。
有什么想法吗?谢谢!
你写的地方
for (i in one) {
for (j in 1:(length(two)-1)) {
B <- NULL
应该是
for (i in one) {
B <- NULL
for (j in 1:(length(two)-1)) {
有输出
> listofdfs
$a
ID x y tag
1 01 2009 a 0
12 05 2010 a 0
14 06 2010 a 0
6 03 2011 a 0
121 05 2010 a 0
141 06 2010 a 0
2 01 2012 a 0
61 03 2011 a 0
$b
ID x y tag
4 02 2009 b 0
9 04 2010 b 0
10 04 2010 b 0
91 04 2010 b 0
101 04 2010 b 0
15 06 2011 b 0
151 06 2011 b 0
$c
ID x y tag
8 04 2009 c 0
13 05 2009 c 0
11 05 2011 c 0
5 03 2012 c 0
111 05 2011 c 0