Quantcast
Viewing all articles
Browse latest Browse all 206180

Using a for loop to create new datasets in the environment

I am trying to use a for loop to filter a large dataset by year. I then want to create new datasets in the global environment that I can use for further calculations.

creating list of years

years <- c("2006", "2007", "2008", "2009", "2010", "2011", "2012")

for loop

for (i in years) {
  pill_[[i]] <- tract_pills %>%
  filter(year == i)
  print(paste0("Finish ", i)
}

I can get this to work, but it doesn't create the datasets for each year. It only create the dataset for the last year.


Viewing all articles
Browse latest Browse all 206180

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>