John Huang
John Huang

Reputation: 845

Iterating a process across multiple years

I have data with multiple years, and I have want to separate each year into summer and winter months. I have been able to do this with one year at a time, but I was wondering if there is a more efficient way of doing this. Is there a way to iterate what I have for a single year at a time across every year?

library(lubridate)
library(tidyverse)

date <- rep_len(seq(dmy("01-01-2010"), dmy("31-12-2013"), by = "days"),1000)
ID <- rep(seq(1, 5), 100)

df <- data.frame(date = date,
                 x = runif(length(date), min = 60000, max = 80000),
                 y = runif(length(date), min = 800000, max = 900000),
                 ID)

df$month <- month(df$date)
df$year <- year(df$date)

df1 <- df %>%
  mutate(season_categ = case_when(month %in% 6:8 ~ 'summer',
                                  month %in% 1:3 ~ 'winter')) %>%
  group_by(ID, year, season_categ) 

summer_2013 <- df1 %>% 
  group_by(ID) %>% 
  filter(year == "2010" & season_categ == "summer")

winter_2013 <- df1 %>% 
  group_by(ID) %>% 
  filter(year == "2010" & season_categ == "winter")

Upvotes: 1

Views: 194

Answers (1)

akrun
akrun

Reputation: 887223

We could filter by 'season_categ' and then use group_split (or split from base R) to return a list of datasets

library(dplyr)
winter_list <- df1 %>% 
                 filter(season_categ == 'winter') %>%
                 group_split(year)


summer_list <- df1 %>% 
                 filter(season_categ == 'summer') %>%
                 group_split(year)

Or with base R

winter_sub <- subset(df1, season_categ == 'winter')
winter_list <- split(winter_sub, winter_sub$year)

summer_sub <- subset(df1, season_categ == 'summer')
summer_list <- split(winter_sub, summer_sub$year)

Then, we can loop over the list with lapply and apply the functions

lapply(summer_list, function(x) yourfun(x))

Upvotes: 2

Related Questions