Cómo convertir un bucle for a una función en R
Estoy intentando convertir un bucle for en una función para las estadísticas resumidas por grupo basadas en los valores de “país” y “continente”. Cualquier ayuda sería apreciada, muchas gracias de antemano.
biblioteca(gapminder)
biblioteca(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
Respuesta esperada
mi.funcion(gapminder, c(“país”, “continente”))
davy.ai
{
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)
}
return(df)
}
{
cont <- unique(gapminder$continente) df <- NULL temp <- NULL para(i en 1:(longitud(cont))) { temp <- gapminder[gapminder$continente == cont[i], ] df[[i]] <- temp %>% dividir(.$continente) %>% map(resumen)
}
return(df)
}