0

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.

lecreprays
  • 77
  • 1
  • 13
  • You need to do `==Year_sub[i]` instead of `=="i"` to access element `i` of you year list `Year_sub`. – Lamia May 10 '17 at 22:54
  • 7
    Are you sure that you need to create a list like this? I suspect `group_by(Year)` might achieve the same results more simply, that's the standard way of carrying out analyses for each subset in `dplyr`. – Marius May 10 '17 at 22:54
  • 7
    I agree with Marius - this is pointless given you are using dplyr already and it has the `group_by` functionality. Also, `split(Weather_Data, Weather_Data$Year)` will give you your intended result with no packages at all. – thelatemail May 10 '17 at 22:55
  • @Marius OK, thanks. Can you then used the group_by df when creating plots in ggplot2? – lecreprays May 10 '17 at 23:06
  • 1
    Agree with previous comments. You have what you need to perform operations on subsets without creating a new data structure. – neilfws May 10 '17 at 23:07
  • @lecreprays: Yes, see questions like this: http://stackoverflow.com/questions/29034863/apply-a-ggplot-function-per-group-with-dplyr-and-set-title-per-group . – Marius May 10 '17 at 23:09

1 Answers1

1

lapply iterates over the first argument, passing each value to the defined function, and the builds a list of the results, but @thelatemail's split is better

lapply(unique(df$Year), function(yr) {df[df$Year==yr,]})
Andrew Lavers
  • 4,328
  • 1
  • 12
  • 19