Skip to content
Prev 392284 / 398502 Next

Extract time and state of charge (Start and End) and Count

Hello,

There was a bug in the way I copied&pasted your data to my R session, 
hence the NA's.

Here is a tidyverse way of doing what you want. Its output matches the 
expected output in your last post. The column names don't start at zero 
because there was no Starting_SoC_of_12 equal to 0.


library(tidyverse)

result <- dt_2014 %>%
   mutate(Hour = lubridate::hour(BatteryChargeStartDate)) %>%
   group_by(Hour, Starting_SoC_of_12, Ending_SoC_of_12) %>%
   mutate(Count = n()) %>%
   ungroup() %>%
   arrange(Hour, Starting_SoC_of_12, Ending_SoC_of_12) %>%
   pivot_wider(
     id_cols = Hour,
     names_from = c(Starting_SoC_of_12, Ending_SoC_of_12),
     names_sep = "-",
     values_from = Count,
     values_fill = 0L
   )

i <- str_order(names(result)[-1], numeric = TRUE)
result <- cbind(result[1], result[-1][i])
result
#  Hour 1-11 2-10 2-11 4-4 4-12 5-8 8-12
#1    7    0    0    0   0    0   1    0
#2    8    0    0    0   0    1   0    1
#3   15    0    0    0   1    0   0    0
#4   16    1    1    0   0    0   0    0
#5   18    0    0    1   0    0   0    1
#6   21    0    0    0   0    1   0    1


Hope this helps,

Rui Barradas


?s 16:10 de 19/07/2022, roslinazairimah zakaria escreveu: