I have a weather dataset which contains observations over multiple years. What I'd like to do is create some sort of loop which will allow me to generically subset my original dataset into sub-datasets for a given year.
Let's call my dataset Weather_Data. Here's some simple example data:
Weather_Data
Year GPS_Coord
2012 x1
2012 x2
2013 x3
2013 x4
2014 x5
2014 x6
2015 x7
2015 x8
In trying to create some sort of loop, I started with a code snippet such as this:
Weather_Data_2012<-Weather_Data%>%filter(Year=="2012")
This code works fine. When trying to create a loop of some sort, however, I tried doing something like this:
Year_list<-list()
Year_sub<-as.character(c(2012:2015))
for (i in 1:length(Year_sub)){
Year_list[[i]]<-Weather_Data%>%filter(Year=="i")
}
When I execute this code, I end up with this output:
A tibble: 0 × 11
So, clearly the loop didn't work as intended!
Here's what I'd like to accomplish via this code:
Year_list[[1]]
Year GPS_Coord
2012 x1
2012 x2
Year_list[[2]]
Year GPS_Coord
2013 x3
2013 x4
Year_list[[3]]
Year GPS_Coord
2014 x5
2014 x6
Year_list[[4]]
Year GPS_Coord
2015 x7
2015 x8
Any tips? Thanks.