Home > OS >  Convert Calendar Entries to Time Per Day by Activity
Convert Calendar Entries to Time Per Day by Activity

Time:10-12

Right now I have a Data Set in R that looks like this:

   date       Activity minutes  hours
 1 2021-09-29 run          35  0.583
 2 2021-09-30 drive        10  0.167
 3 2021-09-30 run          90  1.5  
 4 2021-09-30 sleep       540  9    
 5 2021-10-01 drive        15  0.25 
 6 2021-10-01 sleep       500  8.33 
 7 2021-10-02 sleep       600  10    
 8 2021-10-03 run          55  0.917
 9 2021-10-03 sleep       420  7    
10 2021-10-04 sleep       500  8.33 

I want to convert it to a data set that sums up the hours spent running per day and the hours spent sleeping per day:

   date       time_running time_sleeping
 1 2021-09-29 
 2 2021-09-30 
 3 2021-10-01 
 4 2021-10-02 
 5 2021-10-03 
 6 2021-10-04 

Anyone have any ideas? Thanks!

CodePudding user response:

Not sure of the result you want, but I'm giving it a try! So please find below a little reprex with the package data.table:

REPREX

  • Code
library(data.table)
dcast(DT, date ~ Activity, value.var = "hours")
  • Output:
#>          date drive   run sleep
#> 1: 2021-09-29    NA 0.583    NA
#> 2: 2021-09-30 0.167 1.500  9.00
#> 3: 2021-10-01 0.250    NA  8.33
#> 4: 2021-10-02    NA    NA 10.00
#> 5: 2021-10-03    NA 0.917  7.00
#> 6: 2021-10-04    NA    NA  8.33
  • Data:
DT <- data.table(date = c("2021-09-29", "2021-09-30", "2021-09-30", "2021-09-30",
                          "2021-10-01", "2021-10-01", "2021-10-02", "2021-10-03",
                          "2021-10-03", "2021-10-04"),
                 Activity = c("run", "drive", "run", "sleep", "drive", "sleep",
                              "sleep", "run", "sleep", "sleep"),
                 minutes = c(35, 10, 90, 540, 15, 500, 600, 55, 420, 500),
                 hours = c(0.583, 0.167, 1.5, 9, 0.25, 8.33, 10, 0.917, 7, 8.33))
DT
#>           date Activity minutes  hours
#>  1: 2021-09-29      run      35  0.583
#>  2: 2021-09-30    drive      10  0.167
#>  3: 2021-09-30      run      90  1.500
#>  4: 2021-09-30    sleep     540  9.000
#>  5: 2021-10-01    drive      15  0.250
#>  6: 2021-10-01    sleep     500  8.330
#>  7: 2021-10-02    sleep     600 10.000
#>  8: 2021-10-03      run      55  0.917
#>  9: 2021-10-03    sleep     420  7.000
#> 10: 2021-10-04    sleep     500  8.330

Created on 2021-10-11 by the reprex package (v0.3.0)

  • Related