Column `x` is of unsupported type quoted call

Welcome to the community!

For your future questions, please provide a REPRoducible EXample of your problem. If you don't know how to do it, take a look at this thread.

One problem with your code is that there are problems with parentheses. Also, if you use case_when, all RHS must be of same type. You can't have some of difftime and some of numeric.

Please note that your expected output contains some extra rows compared to the provided input. In my answer below, I've considered all rows in the expected dataset. But it doesn't match your expectation. You want 1 in 1^{st}, 5^{th} and 6^{th} rows. But for 1^{st} and 6^{th} rows, d = b. Hence, they violate the condition: (b == 0 & c %in% V & d != c & d != b). For 5^{th} row, d \neq 0 and hence violate (b %in% V & c != b & d == 0). Please check.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

dataset <- tibble(a = as_datetime(x = 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"),
                                  tz = "GMT"),
                  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))

V <- c(91, 200, 250, 501, 511, 522)

dataset %>%
  mutate(DateShift = lag(x = a),
         Wait = case_when((((b == 0) & (c %in% V) & (d != c) & (d != b)) | ((b %in% V) & (c != b) & (d == 0))) ~ as.difftime(tim = 1,
                                                                                                                             units = "secs"),
                          ((b %in% V) & (c == b) & (d != c)) ~ difftime(time1 = a,
                                                                        time2 = DateShift,
                                                                        units = "secs"),
                          TRUE ~ as.difftime(tim = 0,
                                             units = "secs")))
#> # A tibble: 11 x 6
#>    a                       b     c     d DateShift           Wait   
#>    <dttm>              <dbl> <dbl> <dbl> <dttm>              <drtn> 
#>  1 2018-06-01 09:00:17     0   511     0 NA                   0 secs
#>  2 2018-06-01 20:31:54     0   250   522 2018-06-01 09:00:17  1 secs
#>  3 2018-06-01 20:32:49    91   250     0 2018-06-01 20:31:54  1 secs
#>  4 2018-06-01 20:34:45     0   250   559 2018-06-01 20:32:49  1 secs
#>  5 2018-06-02 12:28:27   522   501   200 2018-06-01 20:34:45  0 secs
#>  6 2018-06-04 22:01:58     0   511     0 2018-06-02 12:28:27  0 secs
#>  7 2018-06-05 01:31:34     0   501   501 2018-06-04 22:01:58  0 secs
#>  8 2018-06-05 01:31:46   501   501     0 2018-06-05 01:31:34 12 secs
#>  9 2018-06-05 01:33:03     0   501   501 2018-06-05 01:31:46  0 secs
#> 10 2018-06-05 01:33:03   501   501   501 2018-06-05 01:33:03  0 secs
#> 11 2018-06-05 01:33:12   501   501     0 2018-06-05 01:33:03  9 secs

Created on 2019-07-10 by the reprex package (v0.3.0)

Hope this helps.

2 Likes