0

I have 5 tibbles for successive years 2016 to 2020. I am doing the same thing to each of the sets of tibbles so I want to use a for-loop rather than copying and pasting the same code 5 times. I have named the tibbles in the following way with the final number indicating the year of the data:

  • alpha_20
  • beta_20
  • gamma_20
  • delta_20
  • epsilon_20

My thought was to do this:

for (i in 16:20) {
    alpha_a_[i]<-alpha_[i]%>%
    mutate(NEWVAR=1+OLDVAR)%>%
    select(NEWVAR, VAR2, VAR3)
    
    beta_a_[i]<-beta_[i]%>%
    group_by(PIN)%>%
    summarize(sum(VAR1))
    
    # and so on for all 5 tibbles
    
    }

But I think I am not calling the tibble correctly because the code breaks at the first mutate. I can't seem to figure out how to instruct it to take the tibbles ending in "16" and then the tibbles ending in "17" and so on.

knesse
  • 185
  • 1
  • 6
  • Hi Knesse. Welcome to SO! Your question contains code, but it is difficult to help you without some test data so we can run your code and examine where/why it fails. Here is a link on how to make a reproducible example. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Steen Harsted Sep 22 '20 at 09:37
  • Thank you! As you say, I am new to posting here. The feedback is helpful. – knesse Sep 24 '20 at 20:04

2 Answers2

0

You can try a combination of get, assign and paste.

for (i in 16:20) {
  alpha <- get(paste("alpha_", i, sep = "")) %>%
    mutate(NEWVAR = 1 + OLDVAR) %>%
    select(NEWVAR, VAR2, VAR3)
  assign(paste("alpha_a_", i, sep = ""), alpha)
  
  
  beta <- get(paste("beta_", i, sep = "")) %>%
    group_by(PIN) %>%
    summarize(sum(VAR1))
  assign(paste("beta_a_", i, sep = ""), beta)
  
  # and so on for all 5 tibbles
  
}
tamtam
  • 3,541
  • 1
  • 7
  • 21
0

There's a couple things going on here. First, in order to actually call the name of your tibble, you're going to want use the get() function on the string name. Try typing "alpha_20" vs. get("alpha_20") in the command line. However, the way you have it coded now as alpha_[i] won't generate the string you want. To generate the name of your tibble as a string, you're going to need to do something like get(paste0("alpha_", i)).

That's all just to get the tibble you want. To edit/save it within the for loop, look into the assign() command (see Change variable name in for loop using R). So all in all, your code will look something like this:

>   require(tidyverse)
>   alpha_20 <- data.frame(x = 1:5, y = 6:10)
>   alpha_20
  x  y
1 1  6
2 2  7
3 3  8
4 4  9
5 5 10
>   
>   for (i in 20) {
+     assign(paste0('alpha_', i), 
+            get(paste0('alpha_', i)) %>% 
+              mutate(z = 11:15))
+   }
>   alpha_20
  x  y  z
1 1  6 11
2 2  7 12
3 3  8 13
4 4  9 14
5 5 10 15
cbowers
  • 137
  • 8