Using for loop to combine a filter and rbind to a new data frame

50 views Asked by At

Raw file

I have data as shown in the image, I want to filter the data by the year column, while ensuring that the months are continuous as in the picture below

filtered file, the format I want

I accomplished the above using the following

gs2_1959 <- filter(g2, Year == "1959/60")
gs2_1960 <- filter(g2, Year == "1960/61")
temp <- rbind(gs2_1959, gs2_1960)

But if there are many years this would be a painful process as I would have to filter for each year.

I have been trying with the for loop like this

for (i in seq_along(gs2_unique)){
    gs_filter <- filter(g2, i %in% gs2_unique)
    View(gs_filter)
} # where unique is the unique years in my data frame.

This retains the years in the original format (see the Raw data image). Raw image

1

There are 1 answers

0
Mark On

Here is how I would do this: instead of having the date across three different columns, combine the Year, Day and Months column into one:

df |>
  mutate(Date = as.Date(paste0(str_extract(Year, "^\\d{4}"), months, Day), "%Y%b%d"),
        Date = if_else(month(Date) < 7, Date + years(1), Date), .before = 1, .keep = "unused") |> # I assume because it looks like football season data, the early year dates are going to be for the first half of the second year, not the first year, so we add one
        arrange(Date)

Output:

         Date  flow
1  1959-10-23 9.328
2  1959-10-24 7.868
3  1959-10-25 7.424
4  1959-10-26 7.024
5  1959-10-27 6.263
6  1959-10-28 5.739
7  1959-10-29 5.241
8  1959-10-30 5.090
9  1959-10-31 4.646
10 1960-10-01 6.263
11 1960-10-02 5.526
12 1960-10-03 5.090
13 1960-10-04 4.623
14 1960-10-05 4.882
15 1960-10-06 5.500