Home > Software engineering >  How to convert a for loop to function in R
How to convert a for loop to function in R

Time:11-18

I am trying to convert for loop to function for the group summary statistics based on the c("country", "continent"). Any help would be appreciated, many thanks in advance.

library(gapminder)
library(purr)
cont <- unique(gapminder$continent)
df <- NULL
temp <- NULL
for(i in 1:(length(cont))) {
  temp <- gapminder[gapminder$continent == cont[i], ]
  #df[[i]] <- temp
  df[[i]] <- temp %>% split(.$continent) %>% map(summary)
  
}
df

Expected Answer

my.function(gapminder, c("country", "continent"))

CodePudding user response:

Just wrap it in a function.

my.function <- function(gapminder, cols) {
  cont <- unique(gapminder$continent)
  df <- NULL
  temp <- NULL
  for(i in 1:(length(cont))) {
    temp <- gapminder[gapminder$continent == cont[i], ]
    df[[i]] <- temp %>% split(.$continent) %>% map(summary)
  }
  df
}

result <- my.function(gapminder, c("country", "continent"))
  

If you name your list "df", then others expect it to be a dataframe. I suggest you rename the variable.

CodePudding user response:

What about a dplyr and purrr solution?

library(dplyr)
library(tidyr)

gapminder %>% 
  group_by(country, continent) %>% 
  group_split(.keep = TRUE) %>% 
  map(summary)
  • Related