1

I'm trying to aggregate the hourly data step_count_full to daily step counts.

 step_count_full
 #> # A tibble: 5,448 x 4
 #>    date_time           date       count location 
 #>    <dttm>              <date>     <dbl> <chr>    
 #>  1 2019-01-01 09:00:00 2019-01-01   764 Melbourne
 #>  2 2019-01-01 10:00:00 2019-01-01   913 Melbourne
 #>  3 2019-01-02 00:00:00 2019-01-02     9 Melbourne
 #>  4 2019-01-02 10:00:00 2019-01-02  2910 Melbourne
 #>  5 2019-01-02 11:00:00 2019-01-02  1390 Melbourne
 #>  6 2019-01-02 12:00:00 2019-01-02  1020 Melbourne
 #>  7 2019-01-02 13:00:00 2019-01-02   472 Melbourne
 #>  8 2019-01-02 15:00:00 2019-01-02  1220 Melbourne
 #>  9 2019-01-02 16:00:00 2019-01-02  1670 Melbourne
 #> 10 2019-01-02 17:00:00 2019-01-02  1390 Melbourne
 #> # … with 5,438 more rows

expected output

 step_count_daily
 #> # A tibble: 364 x 2
 #>    date       daily_count
 #>    <date>           <dbl>
 #>  1 2019-01-01       1677 
 #>  2 2019-01-02      14987 
 #>  3 2019-01-03       9424 
 #>  4 2019-01-04          9 
 #>  5 2019-01-05       7359 
 #>  6 2019-01-06         21 
 #>  7 2019-01-07       9057.
 #>  8 2019-01-08      10341 
 #>  9 2019-01-09       6394 
 #> 10 2019-01-10       7489 
 #> # … with 354 more rows

I tried the below code but not sure how to deal with daily_count.

 step_count_daily <- step_count_full
 step_count_daily <- group_by(step_count_daily) %>% 
 summarise(date=unique(date),daily_count=count )
 step_count_daily 
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
user8016886
  • 19
  • 1
  • 6

1 Answers1

2

Hopefully this works for your purpose:

library(dplyr)

step_count_daily <- step_count_full %>%
  group_by(date) %>%
  add_count(count) %>%
  rename(daily_count = n) %>%
  select(date, daily_count)

step_count_daily

And if you wan to do it with summarise function:

step_count_daily <- step_count_full %>%
  group_by(date) %>%
  summarise(daily_count = sum(count))

step_count_daily
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41