roslinazairimah zakaria
2022-Jul-18 04:04 UTC
[R] Extract time and state of charge (Start and End) and Count
Dear all, I have data of Battery Electric vehicle (BEV). I would like to extract data from every hour starting from 0.00 to 0.59, 1:00-1:59 for SOC(state of charge) start to end. Some examples: I can extract data from SOC=0 and SOC=12 dt_2014[which(dt_2014$Starting_SoC_of_12==0 & dt_2014$Ending_SoC_of_12==12),] I can extract data from SOC=1 and SOC=12 dt_2014[which(dt_2014$Starting_SoC_of_12==1 & dt_2014$Ending_SoC_of_12==12),] and I would like to further categorise the data by hour and count how many cars from 0 state charge to 12 state charge at in that particular hour. Thank you so much for any help given. Some data> dput(dt_2014[1:10,])structure(list(?..CarID = c("GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10"), BatteryChargeStartDate c("16/2/2014 16:05", "16/2/2014 18:20", "17/2/2014 8:10", "18/2/2014 7:41", "18/2/2014 15:36", "18/2/2014 16:36", "18/2/2014 21:26", "19/2/2014 8:57", "19/2/2014 21:08", "20/2/2014 18:11"), BCStartTime = c("16:05", "18:20", "8:10", "7:41", "15:36", "16:36", "21:26", "8:57", "21:08", "18:11"), Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Month = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day = c(16L, 16L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 20L), BatteryChargeStopDate = c("16/2/2014 17:05", "16/2/2014 19:00", "17/2/2014 15:57", "18/2/2014 9:52", "18/2/2014 15:39", "18/2/2014 17:36", "19/2/2014 1:55", "19/2/2014 14:25", "20/2/2014 5:17", "20/2/2014 23:20" ), BCStopTime = c("17:05", "19:00", "15:57", "9:52", "15:39", "17:36", "1:55", "14:25", "5:17", "23:20"), Year2 = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L ), Month2 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day2 = c(16L, 16L, 17L, 18L, 18L, 18L, 19L, 19L, 20L, 20L), Starting_SoC_of_12 c(1L, 2L, 4L, 5L, 4L, 2L, 8L, 8L, 4L, 8L), Ending_SoC_of_12 = c(11L, 11L, 12L, 8L, 4L, 10L, 12L, 12L, 12L, 12L)), row.names = c(NA, 10L), class = "data.frame") -- *Roslinazairimah Zakaria* *Tel: +609-5492370; Fax. No.+609-5492766* *Email: roslinaump at gmail.com <roslinaump at gmail.com>* University Malaysia Pahang Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia [[alternative HTML version deleted]]
@vi@e@gross m@iii@g oii gm@ii@com
2022-Jul-18 04:59 UTC
[R] Extract time and state of charge (Start and End) and Count
It is late here so I will just say I was able to look at your sample data. There may be many approaches here for what I think you want but for simplicity, assume you may want to create a new column or two to use in your analysis. Your variable names are long and hard to read so I won't use them. I am addressing where you want to categorize the data by the hour. My suggestion is you make new columns where you truncate the hour string to something like 1 or 15 so you can later group the data by, among other things, the hour. The next part of your question is less easy to understand as none of the data you supplied had an example of Starting_SoC_of_12 Ending_SoC_of_12 going from 0 to 12. Bottom line is you can make a new column that has some value like TRUE or 1 or whatever if your condition holds that the start was 0 and the end was 12 in that row, otherwise a FALSE or 0 or whatever. I am not sure what you mean by HOW MANY CARS as your example shows all cars having the same ID. But assuming the rest of your data has what you want, then you want to use whatever means you can to cluster the data. I like the tidyverse/dplyr methods but other ways in base R will do too. Bottom line is you take your enhanced data.frame and group by various "date" parts as well as the hour column you made and the column about whether it met your condition. Grouping sort of means there is a way to look at each group. In dplyr, once grouped, you can make a sort of report with summarize where it will show each date and hour along with he number of items in that group. In pseudocode: Filter( keep only rows where the condition variable above is TRUE) Group_by(date, hour) Summarize(full_charge=n()) The above and more are normally done in a pipeline but that is ONE of many approaches. Note that temporary variables can often be avoided as in the filter above but may be useful if you do many kinds of analysis. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of roslinazairimah zakaria Sent: Monday, July 18, 2022 12:04 AM To: R help Mailing list <r-help at r-project.org> Subject: [R] Extract time and state of charge (Start and End) and Count Dear all, I have data of Battery Electric vehicle (BEV). I would like to extract data from every hour starting from 0.00 to 0.59, 1:00-1:59 for SOC(state of charge) start to end. Some examples: I can extract data from SOC=0 and SOC=12 dt_2014[which(dt_2014$Starting_SoC_of_12==0 & dt_2014$Ending_SoC_of_12==12),] I can extract data from SOC=1 and SOC=12 dt_2014[which(dt_2014$Starting_SoC_of_12==1 & dt_2014$Ending_SoC_of_12==12),] and I would like to further categorise the data by hour and count how many cars from 0 state charge to 12 state charge at in that particular hour. Thank you so much for any help given. Some data> dput(dt_2014[1:10,])structure(list(?..CarID = c("GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10", "GC10"), BatteryChargeStartDate c("16/2/2014 16:05", "16/2/2014 18:20", "17/2/2014 8:10", "18/2/2014 7:41", "18/2/2014 15:36", "18/2/2014 16:36", "18/2/2014 21:26", "19/2/2014 8:57", "19/2/2014 21:08", "20/2/2014 18:11"), BCStartTime = c("16:05", "18:20", "8:10", "7:41", "15:36", "16:36", "21:26", "8:57", "21:08", "18:11"), Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Month = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day = c(16L, 16L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 20L), BatteryChargeStopDate = c("16/2/2014 17:05", "16/2/2014 19:00", "17/2/2014 15:57", "18/2/2014 9:52", "18/2/2014 15:39", "18/2/2014 17:36", "19/2/2014 1:55", "19/2/2014 14:25", "20/2/2014 5:17", "20/2/2014 23:20" ), BCStopTime = c("17:05", "19:00", "15:57", "9:52", "15:39", "17:36", "1:55", "14:25", "5:17", "23:20"), Year2 = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L ), Month2 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day2 = c(16L, 16L, 17L, 18L, 18L, 18L, 19L, 19L, 20L, 20L), Starting_SoC_of_12 = c(1L, 2L, 4L, 5L, 4L, 2L, 8L, 8L, 4L, 8L), Ending_SoC_of_12 = c(11L, 11L, 12L, 8L, 4L, 10L, 12L, 12L, 12L, 12L)), row.names = c(NA, 10L), class = "data.frame") -- *Roslinazairimah Zakaria* *Tel: +609-5492370; Fax. No.+609-5492766* *Email: roslinaump at gmail.com <roslinaump at gmail.com>* University Malaysia Pahang Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Rui Barradas
2022-Jul-18 17:11 UTC
[R] Extract time and state of charge (Start and End) and Count
Hello, I'm not sure I understand the problem. Do you want counts of how many rows are there per hour? # these columns need to be fixed cols <- c("BatteryChargeStartDate", "BatteryChargeStopDate") dt_2014[cols] <- lapply(dt_2014[cols], \(x) sub("\n", " ", x)) # use package lubridate to coerce to a datetime class dt_2014[cols] <- lapply(dt_2014[cols], lubridate::dmy_hm) h <- lubridate::hour(dt_2014[["BatteryChargeStartDate"]]) aggregate(Starting_SoC_of_12 ~ h, dt_2014, length) It would be better if you post the expected output corresponding to the posted data set. Hope this helps, Rui Barradas ?s 05:04 de 18/07/2022, roslinazairimah zakaria escreveu:> Dear all, > > I have data of Battery Electric vehicle (BEV). I would like to extract data > from every hour starting from 0.00 to 0.59, 1:00-1:59 for SOC(state of > charge) start to end. > > Some examples: > I can extract data from SOC=0 and SOC=12 > dt_2014[which(dt_2014$Starting_SoC_of_12==0 & > dt_2014$Ending_SoC_of_12==12),] > > I can extract data from SOC=1 and SOC=12 > dt_2014[which(dt_2014$Starting_SoC_of_12==1 & > dt_2014$Ending_SoC_of_12==12),] > > and I would like to further categorise the data by hour and count how many > cars from 0 state charge to 12 state charge at in that particular hour. > > Thank you so much for any help given. > > Some data >> dput(dt_2014[1:10,]) > structure(list(?..CarID = c("GC10", "GC10", "GC10", "GC10", "GC10", > "GC10", "GC10", "GC10", "GC10", "GC10"), BatteryChargeStartDate > c("16/2/2014 16:05", > "16/2/2014 18:20", "17/2/2014 8:10", "18/2/2014 7:41", "18/2/2014 15:36", > "18/2/2014 16:36", "18/2/2014 21:26", "19/2/2014 8:57", "19/2/2014 21:08", > "20/2/2014 18:11"), BCStartTime = c("16:05", "18:20", "8:10", > "7:41", "15:36", "16:36", "21:26", "8:57", "21:08", "18:11"), > Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, > 2014L, 2014L, 2014L), Month = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, > 2L, 2L, 2L), Day = c(16L, 16L, 17L, 18L, 18L, 18L, 18L, 19L, > 19L, 20L), BatteryChargeStopDate = c("16/2/2014 17:05", "16/2/2014 > 19:00", > "17/2/2014 15:57", "18/2/2014 9:52", "18/2/2014 15:39", "18/2/2014 > 17:36", > "19/2/2014 1:55", "19/2/2014 14:25", "20/2/2014 5:17", "20/2/2014 23:20" > ), BCStopTime = c("17:05", "19:00", "15:57", "9:52", "15:39", > "17:36", "1:55", "14:25", "5:17", "23:20"), Year2 = c(2014L, > 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L > ), Month2 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day2 = c(16L, > 16L, 17L, 18L, 18L, 18L, 19L, 19L, 20L, 20L), Starting_SoC_of_12 > c(1L, > 2L, 4L, 5L, 4L, 2L, 8L, 8L, 4L, 8L), Ending_SoC_of_12 = c(11L, > 11L, 12L, 8L, 4L, 10L, 12L, 12L, 12L, 12L)), row.names = c(NA, > 10L), class = "data.frame") > >
roslinazairimah zakaria
2022-Jul-19 02:46 UTC
[R] Extract time and state of charge (Start and End) and Count
Thank you so much Avi, Rui and Jim...really appreciate your help. I am so sorry for not able to make it clearly. This is the expected output: Hour Starting_SoC_of_12 Ending_SoC_of_12 frequency 0 0 12 603 1 1 12 136 dt <- dt3[which(dt3$Year==2014),] head(dt); tail(dt) dput(dt[1:5000,]) cols <- c("BatteryChargeStartDate", "BatteryChargeStopDate") dt[cols] <- lapply(dt[cols], \(x) sub("\n", " ", x)) # use package lubridate to coerce to a datetime class dt[cols] <- lapply(dt[cols], lubridate::dmy_hm) h <- lubridate::hour(dt[["BatteryChargeStartDate"]]) aggregate(Starting_SoC_of_12 ~ h, dt, length) Expected output: Hour Starting_SoC_of_12 Ending_SoC_of_12 frequency 0 0 12 603 1 1 12 136 On Mon, Jul 18, 2022 at 12:04 PM roslinazairimah zakaria < roslinaump at gmail.com> wrote:> Dear all, > > I have data of Battery Electric vehicle (BEV). I would like to extract > data from every hour starting from 0.00 to 0.59, 1:00-1:59 for SOC(state of > charge) start to end. > > Some examples: > I can extract data from SOC=0 and SOC=12 > dt_2014[which(dt_2014$Starting_SoC_of_12==0 & > dt_2014$Ending_SoC_of_12==12),] > > I can extract data from SOC=1 and SOC=12 > dt_2014[which(dt_2014$Starting_SoC_of_12==1 & > dt_2014$Ending_SoC_of_12==12),] > > and I would like to further categorise the data by hour and count how many > cars from 0 state charge to 12 state charge at in that particular hour. > > Thank you so much for any help given. > > Some data > > dput(dt_2014[1:10,]) > structure(list(?..CarID = c("GC10", "GC10", "GC10", "GC10", "GC10", > "GC10", "GC10", "GC10", "GC10", "GC10"), BatteryChargeStartDate > c("16/2/2014 16:05", > "16/2/2014 18:20", "17/2/2014 8:10", "18/2/2014 7:41", "18/2/2014 15:36", > "18/2/2014 16:36", "18/2/2014 21:26", "19/2/2014 8:57", "19/2/2014 21:08", > "20/2/2014 18:11"), BCStartTime = c("16:05", "18:20", "8:10", > "7:41", "15:36", "16:36", "21:26", "8:57", "21:08", "18:11"), > Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, > 2014L, 2014L, 2014L), Month = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, > 2L, 2L, 2L), Day = c(16L, 16L, 17L, 18L, 18L, 18L, 18L, 19L, > 19L, 20L), BatteryChargeStopDate = c("16/2/2014 17:05", "16/2/2014 > 19:00", > "17/2/2014 15:57", "18/2/2014 9:52", "18/2/2014 15:39", "18/2/2014 > 17:36", > "19/2/2014 1:55", "19/2/2014 14:25", "20/2/2014 5:17", "20/2/2014 > 23:20" > ), BCStopTime = c("17:05", "19:00", "15:57", "9:52", "15:39", > "17:36", "1:55", "14:25", "5:17", "23:20"), Year2 = c(2014L, > 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L > ), Month2 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day2 = c(16L, > 16L, 17L, 18L, 18L, 18L, 19L, 19L, 20L, 20L), Starting_SoC_of_12 > c(1L, > 2L, 4L, 5L, 4L, 2L, 8L, 8L, 4L, 8L), Ending_SoC_of_12 = c(11L, > 11L, 12L, 8L, 4L, 10L, 12L, 12L, 12L, 12L)), row.names = c(NA, > 10L), class = "data.frame") > > > -- > *Roslinazairimah Zakaria* > *Tel: +609-5492370; Fax. No.+609-5492766* > > *Email: roslinaump at gmail.com <roslinaump at gmail.com>* > University Malaysia Pahang > Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia >-- *Roslinazairimah Zakaria* *Tel: +609-5492370; Fax. No.+609-5492766* *Email: roslinazairimah at ump.edu.my <roslinazairimah at ump.edu.my>; roslinaump at gmail.com <roslinaump at gmail.com>* Faculty of Industrial Sciences & Technology University Malaysia Pahang Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia [[alternative HTML version deleted]]
Jim Lemon
2022-Jul-19 07:05 UTC
[R] 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:> > Dear all, > > I have data of Battery Electric vehicle (BEV). I would like to extract data > from every hour starting from 0.00 to 0.59, 1:00-1:59 for SOC(state of > charge) start to end. > > Some examples: > I can extract data from SOC=0 and SOC=12 > dt_2014[which(dt_2014$Starting_SoC_of_12==0 & > dt_2014$Ending_SoC_of_12==12),] > > I can extract data from SOC=1 and SOC=12 > dt_2014[which(dt_2014$Starting_SoC_of_12==1 & > dt_2014$Ending_SoC_of_12==12),] > > and I would like to further categorise the data by hour and count how many > cars from 0 state charge to 12 state charge at in that particular hour. > > Thank you so much for any help given. > > Some data > > dput(dt_2014[1:10,]) > structure(list(?..CarID = c("GC10", "GC10", "GC10", "GC10", "GC10", > "GC10", "GC10", "GC10", "GC10", "GC10"), BatteryChargeStartDate > c("16/2/2014 16:05", > "16/2/2014 18:20", "17/2/2014 8:10", "18/2/2014 7:41", "18/2/2014 15:36", > "18/2/2014 16:36", "18/2/2014 21:26", "19/2/2014 8:57", "19/2/2014 21:08", > "20/2/2014 18:11"), BCStartTime = c("16:05", "18:20", "8:10", > "7:41", "15:36", "16:36", "21:26", "8:57", "21:08", "18:11"), > Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, > 2014L, 2014L, 2014L), Month = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, > 2L, 2L, 2L), Day = c(16L, 16L, 17L, 18L, 18L, 18L, 18L, 19L, > 19L, 20L), BatteryChargeStopDate = c("16/2/2014 17:05", "16/2/2014 > 19:00", > "17/2/2014 15:57", "18/2/2014 9:52", "18/2/2014 15:39", "18/2/2014 > 17:36", > "19/2/2014 1:55", "19/2/2014 14:25", "20/2/2014 5:17", "20/2/2014 23:20" > ), BCStopTime = c("17:05", "19:00", "15:57", "9:52", "15:39", > "17:36", "1:55", "14:25", "5:17", "23:20"), Year2 = c(2014L, > 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L > ), Month2 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day2 = c(16L, > 16L, 17L, 18L, 18L, 18L, 19L, 19L, 20L, 20L), Starting_SoC_of_12 > c(1L, > 2L, 4L, 5L, 4L, 2L, 8L, 8L, 4L, 8L), Ending_SoC_of_12 = c(11L, > 11L, 12L, 8L, 4L, 10L, 12L, 12L, 12L, 12L)), row.names = c(NA, > 10L), class = "data.frame") > > > -- > *Roslinazairimah Zakaria* > *Tel: +609-5492370; Fax. No.+609-5492766* > > *Email: roslinaump at gmail.com <roslinaump at gmail.com>* > University Malaysia Pahang > Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.