loop objects

Asked

Viewed 287 times

4

I need to make several regatta .

for example:

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

but I need to perform the same procedure to 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’t make it create multiple tables (2012ocup10,2011ocup10,...).Using get(paste0...) it seems to me that it does not access the variables within the data.frame

1 answer

4


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

With this exception, 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))
}
  • 2

    Thank you very much ,helped VERY ! I had not thought to use assign ,e nor attack the number in the creation of the object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.