Ian_De_Oliveira
Ian_De_Oliveira

Reputation: 291

Changing from a loop to a function in R lapply

I understand that using loops in R is not best practice. I often fail to get them going. I have a list of data frames that I have to do a repetitive task.

This is the list

  list_BG<- list(Education,Exp_intensity,Software_skills,Specialized,Common_Skills)

So far I have done the task manually

example 1:

 Education_sum <-Education %>% 
   mutate(ANZSCO4_CODE = as.numeric(substr(ANZSCO4_CODE, 1, 3)))%>%
   group_by(ANZSCO4_CODE) %>%
   summarise_all(funs(mean))

Then I have tried a loop and fail

 for (i in list_BG) {
   Gen[[i]]<- i %>% 
     mutate(ANZSCO4_CODE = as.numeric(substr(ANZSCO4_CODE, 1, 3)))%>%
     group_by(ANZSCO4_CODE) %>%
     summarise_all(funs(mean))

}

The question then is; why is my loop failing and second how could I transform this loop into a fuicntion to create the 5 data frames I'm trying to create. All help is appreciated.

Upvotes: 0

Views: 60

Answers (1)

zacdav
zacdav

Reputation: 4671

When you use for (i in list_BG) i is actually the whole element (data.frame) and then you are trying to use that same i to create your list Gen.

You should use seq_along to create an index to iterate over as so:

# data
df_list <- split(iris, iris$Species)

# using a for loop with seq_along
ans1 <- list()
for (i in seq_along(df_list)) {
  ans1[[i]] <- df_list[[i]] 
  # additional processing goes here
}

# using lapply, does not require any index in this case
ans2 <- lapply(df_list, function(x) {
  x
  # additional processing goes here
})

You can use purrr::map() instead but I also suspect a combination of dplyr::bind_rows and updating your group_by would be cleaner - I'd need to see the data however.

Upvotes: 2

Related Questions