你可以尝试
library(dplyr)
names <- c("2000", "2001", "2002", "2003", "2004", "2005")
for(n in names){
Temp <- bind_cols( get(paste0('A', n)), get(paste0('B', n)),
get(paste0('C', n)))
assign(paste0('Stake_', n), Temp)
}
identical(cbind(A2000, B2000, C2000), Stake_2000)
#[1] TRUE
identical(cbind(A2005, B2005, C2005), Stake_2005)
#[1] TRUE
For the aggregate
,你可以做
lapply(mget(paste0('A', 2000:2005)), function(x)
aggregate(V1~V2, x, FUN=length))
同样对于B
and C
虽然不清楚是什么S_2000
is.
Update
如果行数不同,我们可以使用combine
with stri_list2matrix
from stringi
A2000 <- rbind(A2000, c(4,8, 9 , 15, 25))
library(stringi)
for(n in names){
Temp <- as.data.frame(stri_list2matrix(combine( get(paste0('A', n)),
get(paste0('B', n)), get(paste0('C', n)))), stringsAsFactors=FALSE)
Temp[] <- lapply(Temp, as.numeric)
assign(paste0('Stake_', n), Temp)
}
Stake_2000
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15
#1 6 19 12 18 1 1 18 5 7 17 9 19 12 18 8
#2 4 5 7 4 11 12 9 1 2 5 4 13 18 5 6
#3 14 16 14 0 15 3 7 13 20 0 4 3 0 0 6
#4 10 16 14 10 2 4 10 6 13 16 4 2 6 8 15
#5 13 5 6 2 4 12 11 0 10 16 9 17 12 7 6
#6 4 8 9 15 25 NA NA NA NA NA NA NA NA NA NA
data
set.seed(24)
list2env(setNames(lapply(1:6, function(i)
as.data.frame(matrix(sample(0:20, 5*5, replace=TRUE), ncol=5))),
paste0('A', 2000:2005)), envir=.GlobalEnv)
list2env(setNames(lapply(1:6, function(i)
as.data.frame(matrix(sample(0:20, 5*5, replace=TRUE), ncol=5))),
paste0('B', 2000:2005)), envir=.GlobalEnv)
list2env(setNames(lapply(1:6, function(i)
as.data.frame(matrix(sample(0:20, 5*5, replace=TRUE), ncol=5))),
paste0('C', 2000:2005)), envir=.GlobalEnv)