I have a lot of different e-cars (car1 ... carN) with a state of charge (SOC) at the beginning ot a trip (start_trip) and the end (end_trip). Between to trips a car is loaded, to a maximum level. I know the charge capacity for the loading of a batterie (~0.35 /minute) an the needed capacity for every kilometer (0.22 / kilometer). Between two trips we have some minutes to load and during a trip the capacity decreases.
I want to estimate the necessary time between to trips to fulfill the need in 99% of the bookings, if i assume to deliver the same service with e-cars in the future.
I have a list of nearly 1 million bookings with combustion vehicles.
carN starttime stoptime drivenkm
I calculate the possible time between the end of a trip and the start of the last use with loadtime = starttime - lag(stoptime) so i know, what`s the SOC_S at the start of a trip.
I calculate the needed capacity for a trip and substract it from SOC_S. So i get the SOC_E at the end of a trip.
Now i want to take in account, that a battery is not really empty at the end of a trip. So, the chargestate is SOC_E from the last trip + possible load between trips.
Now i have some problems:
B_ES <- B_ES %>%
arrange(car, start_trip) %>%
preTime <- (start_trip - lag(end_trip))/60,
useTime <- (end_trip - start_trip)/60,
postTime <- (lead(start_trip) - end_trip)/60,
SOC_S <- preTime * ZOE_charge,
E_consumption <- km * ZOE_consumption,
SOC_E <- SOC_S - E_consumption
SOC_S <- SOC_S + lag(SOC_E)
foo <- data_frame(group = c('A','A','A','B','B','C','C') , x1 = c(1,2,3,1,2,1,2) , SOC_S = c(4,5,6,4,5,4,5))
In regards to your second goal:
- I want to set the first SOC_S for the first booking for every car to 22 kWh. It starts fully loaded. How to put a value in a special column of the first row of a group_by in R for evvery car?
library(tidyverse) dd <- foo %>% group_by(group) %>% mutate(rownumber = 1:n() ## a helper column to find first row , special = ifelse(rownumber == 1, 22, SOC_S)) ## vectorized edits based on rownumber
Note: You can also replace
SOC_S if preferred.
Couldn't really follow the rest of the question but this might get you a little farther.