create looped objects

4

I need to do several aggregate.

For example:

2012ocup10 <- aggregate(PNAD2012[c("peso_pes")], by=PNAD2012["klems"],
                       FUN=sum,na.rm=T)

But I need to do the same procedure for PNAD2011,PNAD2010, ...

I use loop this way:

for(ano in (1995:2012)[-c(6,16)]){   
  tabela.p <- apply(get(paste0("PNAD",ano))[c( "idade", "anos_est", "rend_prin", 
                                               "rend_tot",
                                               "horas", "horas_s")],
                    2,FUN=mean,na.rm=T) 
}

When I adapt pro aggregate I can not get it to create multiple tables (2012ocup10,2011ocup10,...) . Using get (paste0 ...) I can not seem to access the variables inside data.frame

    
asked by anonymous 18.09.2014 / 21:03

1 answer

4

Ennio, you can not create object names that begin with a number. 2012ocup10 is not a valid name for an object of R. You have to at least put a letter or something of the type before in this name, for example, pnad2012ocup10 .

With this caveat, a code like the following should work:

for (ano in (1995:2012)){
  assign(paste0("pnad", ano, "ocup10"),
         aggregate(get(paste0("PNAD", ano))["peso_pes"], 
                   by=get(paste0("PNAD", ano))["klems"],
                   FUN=sum,na.rm=T))
}
    
19.09.2014 / 01:11