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.