Make a loop with dates

Hey everyone! I'm starting to learn by my own how to deal with data and I get stucked when it comes to make a loop that involves dates.

I have a data frame that looks something like this:

      date         time      solar
1  2019-05-17    11:20:00   710
2  2019-05-17    11:25:00   712
3  2019-05-17    11:30:00   787
.
.
.
70 2019-08-06    10:20:00   926
71 2019-08-06    10:25:00   204
72 2019-08-06    10:30:00   498
.
.
.
90 2020-01-17    10:20:00   936
91 2020-01-17    10:25:00   639
92 2020-01-17    10:30:00   876

The data is a collection of every day since 2019-05-17 until 2021-09-16, every 5 minutes I have solar information, so I want to sum the total solar info I have per month-year and save this into a vector, for example sum all solar of '2020-05' and so on...

Here is how I was working:

With the library(tibbletime) first I convert my data frame(df1) into:

df_t<- as_tbl_time(df1, index = date) 

And then

may19 <- filter_time(df_t, ~'2019-05')
jun19 <- filter_time(df_t, ~'2019-06')
jul19 <- filter_time(df_t, ~'2019-07')
ago19 <- filter_time(df_t, ~'2019-08')
sep19 <- filter_time(df_t, ~'2019-09')
oct19 <- filter_time(df_t, ~'2019-10')
nov19 <- filter_time(df_t, ~'2019-11')
dic19 <- filter_time(df_t, ~'2019-12') 

It create a new df filtered, then I just sum the column I want, but it's very hard to do it in this way, I was trying to make a loop that simplifies the work the problem is that I don't know how, because if I do the loop I can't put the i in the: '2019-i'

How would you do it?

Can you provide a reproducible example?

library(tibbletime)
library(lubridate)
data(FB)

our_example <- FB %>% mutate(mdate=floor_date(date,unit = "month"))

our_example

# lets say I want to find the column means for numeric variables for each mdate 
# alternatively to writing a loop I can do :

group_by(our_example,mdate) %>% summarise(across(where(is.numeric),mean))
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.