Skip to content
Prev 392277 / 398502 Next

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

Hi Roslina,
The following gives you the state of charge for the vehicle in your
example data for each hour. This is approximate as your times are not
on even hours.

# get the temporal order of observations
obs_order<-order(c(as.numeric(dt_2014$bc_start),as.numeric(dt_2014$bc_stop)))
numeric_time<-c(as.numeric(dt_2014$bc_start),as.numeric(dt_2014$bc_stop))[obs_order]
nobs<-diff(range(numeric_time))/3600
# find the linear approximation of charge state by hours
hourly_SoC<-approx(numeric_time,
 c(dt_2014$Starting_SoC_of_12,dt_2014$Ending_SoC_of_12)[obs_order],n=nobs)

To get the POSIX times:

hourly_POSIX<-seq(dt_2014$bc_start[1],dt_2014$bc_stop,length.out=nobs)

That will fill part of your table. If you want the state of charge by
hour regardless of day, you'll have to create a new "hour" variable
from, hourly_POSIX, then:

mean_charge_by_hour<-by(hourly_SoC$hour,hourly_SoC$y,mean) #untested
since I don't know whether you want this

Jim

On Mon, Jul 18, 2022 at 2:04 PM roslinazairimah zakaria
<roslinaump at gmail.com> wrote: