2
votes

I need to perform a variety of actions by combining, aggregating and splitting data frames. These actions need to be repeated for several years in a row (2000, 2001, 2002 etc.). However, I can't find a way to refer to multiple data frames based on a looping string with the years.

An example: I want to combine 3 data frames from the same year. My current code:

Stake_2000 <- combine(A2000, B2000, C2000)
Stake_2001 <- combine(A2001, B2001, C2001)
Stake_2002 <- combine(A2002, B2002, C2002)
Stake_2003 <- combine(A2003, B2003, C2003)
Stake_2004 <- combine(A2004, B2004, C2004)
Stake_2005 <- combine(A2005, B2005, C2005)

I would like to simplify by replacing the years by a variable in a loop. However, I cannot manage to let R read from the appropriate data frames. I've stranded in multiple attempts:

names <- c("2000", "2001", "2002", "2003", "2004", "2005")
for (n in names)
{Temp <- combine(c("A",n,sep=""), (c"B",n,sep=""), c("C",n,sep=""))
assign(paste("Stake_",n,sep=""), Temp)}

or replace combine function with combine(An, Bn, Cn), or combine(A+n, B+n, C+n)

Besides these actions, I need to do aggregating and matching from different databases with the similar problems of the years. For example replace all the "2000" with subsequent years in a loop:

Temp <- aggregate(VarA~VarB, data=A_2000, FUN=length)
S_2000$VarC <- Temp[match(S_2000$ID, Temp$ID), "VarA"]

I presume there is some pretty straight forward answer to it, but I haven't been able to find it.

1
I guess you are missing a paste i.e. paste(c('A', n, sep=''). It is better to provide a small reproducible example stackoverflow.com/questions/5963269/…akrun
Thanks. I'll make a reproducable example. Unfortunately inserting the paste does not work either.Matthee le Cointre

1 Answers

1
votes

You could try

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, you could do

 lapply(mget(paste0('A', 2000:2005)), function(x)
                       aggregate(V1~V2, x, FUN=length))

Similarly for B and C though it is not clear what S_2000 is.

Update

If the number of rows are different, may be we can use 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)