Help! Creating Subsets for dates then creating a graph

I have 2 years of daily data That I would like to subset into Date Quarters for example, Jan, Feb, Mar = Q1_2019, Apl, May, Jun=Q2_2019, etc.
I have Already Created a Summary data that has compiled the data into Months. This is the Code I used:

Summary_dates <- mydata %>% group_by(month = lubridate::floor_date(newDate, 'month'),Provider)%>% summarise(avg_steps = mean(Steps), avg_TEE=mean(TEE), avg_activity_min=mean(total_activity_min))

From here I tried Creating my first subset

Q1_2019  <- data[Summary_dates$month >= "2019-01-01" &  Summary_dates$month <= "2019-03-01", ]

But I Keep receiveing an error code:

 Error in data[Summary_dates$month >= "2019-01-01" & Summary_dates$month <=  : 
  object of type 'closure' is not subsettable

After My quarter subsets are created, I want to create a new Dataframe with all the Quarters so I can make a Graph Comparing each quarter to each other.

Here is a Glimpse of mydata :

glimpse(mydata)
Rows: 72,001
Columns: 12
$ ID                 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ Steps              <dbl> 1208, 6784, 6470, 10042, 6301, 9619, 7844, 5718, 6620, 6033, 6337, 4917, 10586, 9199, 96…
$ TEE                <dbl> 2375, 2713, 2685, 2817, 4289, 3405, 3097, 3902, 2810, 2774, 2574, 2841, 3552, 2892, 3233…
$ AEE                <dbl> 72, 410, 382, 514, 1986, 1102, 794, 1599, 507, 471, 271, 538, 1249, 589, 930, 263, 1291,…
$ Sedentary          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ LPA                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ MPA                <dbl> 10, 0, 0, 0, 21, 7, 38, 33, 6, 26, 0, 40, 43, 19, 42, 0, 62, 47, 138, 63, 0, 0, 0, 15, 1…
$ VPA                <dbl> 0, 0, 0, 0, 137, 6, 6, 106, 6, 5, 0, 3, 51, 0, 23, 0, 12, 60, 86, 34, 0, 0, 0, 13, 1, 0,…
$ NonWear            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ Provider           <chr> "Garmin", "Garmin", "Garmin", "Garmin", "Garmin", "Garmin", "Garmin", "Garmin", "Garmin"…
$ newDate            <date> 2019-09-03, 2019-09-04, 2019-09-05, 2019-09-06, 2019-09-07, 2019-09-08, 2019-09-09, 201…
$ total_activity_min <dbl> 10, 0, 0, 0, 158, 13, 44, 139, 12, 31, 0, 43, 94, 19, 65, 0, 74, 107, 224, 97, 0, 0, 0, …

I am thinking your issue lies in here... I see you have data.frames called Summary_dates and mydata. But you are trying to subset using data[.... Have you defined a data.frame called data anywhere? If not, data is a function you get loaded with each R session (see ?utils::data). The type of a function is closure, so R is telling you that you are trying to subset a function, which isn't defined.

Would This:

Q1_2019  <- data[Summary_dates$month >= "2019-01-01" &  Summary_dates$month <= "2019-03-01", ]

Not define the function as Q1_2019?

The problem isn't the Q1_2019, it is the data. When you say data[Summary_dates$month >= '2019-01-01'], what does data refer to?

Well the Data would be coming from the Summary_dates Data Frame, speifically The Month column.

glimpse(Summary_dates)
Rows: 48
Columns: 5
Groups: month [24]
$ month            <date> 2019-01-01, 2019-01-01, 2019-02-01, 2019-02-01, 2019-03-01,~
$ Provider         <chr> "Fitbit", "Garmin", "Fitbit", "Garmin", "Fitbit", "Garmin", ~
$ avg_steps        <dbl> 6688.323, 9478.504, 6977.183, 9554.405, 7371.478, 10015.435,~
$ avg_TEE          <dbl> 1851.242, 2497.954, 1915.043, 2449.411, 1987.841, 2475.102, ~
$ avg_activity_min <dbl> 727.73118, 36.34722, 729.92674, 33.22716, 759.64930, 36.9240~

I'm pretty lost on this :melting_face:

But you have not defined this in code. You are not being asked from where "the data" comes from but from where the object called data is supposed to come from because you haven't defined it in the code you are sharing.

Here you are trying to subsett an onbjec called data but I think you actually want to subset an object called Summary_dates.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

Yes, you're correct! I am trying to subset Summary _dates.
So what would be the correct code to do this? :grinning:

Simply replace the word data by Summary_dates

1 Like

This topic was automatically closed 7 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.