在具有四列的大型数据框(“数据”)中,我必须添加另一列(“等待”),其值有条件地基于前四列。我收到“错误:列k
的引用电话类型不受支持”
我会优先选择dplyr和mutate和Case when答案。
我的数据框如下:
Wait
这是我写的(我不是r的专家):
#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")
我收到以下错误:
“错误:列#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)
的引用电话类型不受支持”
预期结果应为:
Wait
我需要帮助来解决此问题。我也乐于接受其他有关如何达到目标的建议。