Skip to content

Instantly share code, notes, and snippets.

@aammd
Created January 12, 2016 19:18
Show Gist options
  • Save aammd/eade95718b214d4a4fd3 to your computer and use it in GitHub Desktop.
Save aammd/eade95718b214d4a4fd3 to your computer and use it in GitHub Desktop.
simple example of modifying variables with a for loop in R
library(gapminder)
names(gapminder) %>% dput
div_mean <- function(x) x/mean(x)
myvarnames <- c("pop", "gdpPercap")
gapminder2 <- gapminder
for (i in myvarnames){
gapminder2[[i]] <- div_mean(gapminder2[[i]])
}
glimpse(gapminder2); glimpse(gapminder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment