Rüdiger Kladt Rüdiger Kladt - 3 years ago 144
R Question

how to put a value in the first row of a group_by in R

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:


  1. the maximum load is 22 kWh. So the load is the max("loaded charge by time" or "maxLoad". The first is a vector and the secon a constant number. max(a, b) don`t work.

  2. 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?

  3. How can I calculate a value for needed loadtime to fulfill 99% of the use? Something like teh solver in excel?

    B_ES <- B_ES %>%
    arrange(car, start_trip) %>%
    group_by(car) %>%
    mutate(
    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)
    )



This code don`t work. The problem are


  1. How can i use the new variables? There have colnames like this: "preTime <- (Nutzungsbeginn - lag(Nutzungsende))/60"

  2. The last two seems to come to an irregular loop.



Regards

Ruediger

Answer Source

Reproducible Example:

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:

  1. 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 special with SOC_S if preferred.

Couldn't really follow the rest of the question but this might get you a little farther.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download