In a large dataframe ("data") with four columns I have to add another column ("Wait") with values conditionally based on the first four columns. I get the "Error: Column Wait
is of unsupported type quoted call"
I will Prefer answers with dplyr and mutate and Case When.
My dataframe looks like this:
#Creating a sample dataset
#A = Datetime
#a, b and c are error codes
# 0 means successful
data <- data.frame(a = c("2018-06-01 09:00:17",
"2018-06-01 20:31:54",
"2018-06-01 20:32:49",
"2018-06-01 20:34:45",
"2018-06-02 12:28:27",
"2018-06-04 22:01:58",
"2018-06-05 01:33:03",
"2018-06-05 01:33:12"),
b = c(0, 0, 91, 0, 522, 0,501, 501),
c = c(511, 250, 250, 250, 501, 511, 501, 501),
d = c(0, 522, 0, 559, 200, 0, 501, 501),
stringsAsFactors = FALSE)
#Change character to Datetime
data$a <- as_datetime(data$a, tz="GMT")
This is what i have written (i am not an expert in r):
#Error Code Vector
V <- c(91,200,250,501,511,522)
#Adding DateShift Column
data<- data %>%
arrange((a))%>%
mutate(DateShift = lag(a))
data
#Calculating the WaitSecs
data <- data %>%
mutate(Wait = case_when(b == 0 & c %in% V & d != c & d != b) ~ 1,
(b == 0 & c %in% V & d != c & d != b) ~ 1,
(b %in% V & c != b & d == 0) ~ 1,
(b %in% V & c == b & d != c) ~ a - DateShift,
TRUE ~ 0)
I get the error below:
Error: Column Wait
is of unsupported type quoted call
Expected Result Should be:
data <- data.frame(a = c("2018-06-01 09:00:17",
"2018-06-01 20:31:54",
"2018-06-01 20:32:49",
"2018-06-01 20:34:45",
"2018-06-02 12:28:27",
"2018-06-04 22:01:58",
"2018-06-05 01:31:34",
"2018-06-05 01:31:46",
"2018-06-05 01:33:03",
"2018-06-05 01:33:03",
"2018-06-05 01:33:12"),
b = c(0, 0, 91, 0, 522, 0, 0, 501, 0, 501, 501),
c = c(511, 250, 250, 250, 501, 511, 501, 501, 501, 501, 501),
d = c(0, 522, 0, 559, 200, 0, 501, 0, 501, 501,0),
DateShift = c(NA,
"2018-06-01 09:00:17",
"2018-06-01 20:31:54",
"2018-06-01 20:32:49",
"2018-06-01 20:34:45",
"2018-06-02 12:28:27",
"2018-06-04 22:01:58",
"2018-06-05 01:31:34",
"2018-06-05 01:31:46",
"2018-06-05 01:33:03",
"2018-06-05 01:33:03"),
Wait = c(1, 1, 1, 1, 1, 1, 0, 12, 0, 0, 9),
stringsAsFactors = FALSE)
data
I need help to resolve this. I am also open to other suggestions on how to achieve my result.