While I appreciate the type checking done by if_else()
and case_when()
since it can catch dangerous mistakes, I find the lack of automatic conversion between integer and numeric to be quite cumbersome.
For example, in one survey I am using a variable (here y
) is expressed as a number of hours either per day of per week. The time unit is stored in variable x
. In order to compute the number of hours per day, one can use if_else
to perform the division when needed. Unfortunately, the following code doesn't work when y
is of type integer
, which is the case in the dBase files I import using read.dbf
.
library(dplyr)
df <- data.frame(x=c("Week", "Day"), y=c(1L, 2L))
mutate(df, z = if_else(x == "Week", y/7, y))
# Result:
Error in mutate_impl(.data, dots) :
`false` has type 'integer' not 'double'
(In reality, the condition is more complex, so I'd like to use case_when
instead of nested if_else
.)
Would it be possible to allow conversion of integer columns to numeric? There's no risk of generating incorrect data in that case. In general the difference between numeric
and integer
is quite hidden in R, so new users are likely to be confused by this error. I'm feeling quite bad to have to explain this to newcomers to perform such a simple operation.
Can you use as.numeric(y)
instead of y
for now?
@hadley: Are we too strict by not auto-converting integer to numeric in if_else()
and case_when()
?
Can you use as.numeric(y) instead of y for now?
Yes, of course that works, but it's cumbersome, and confusing for newcomers.
Yes, this is a known issue and will (eventually) be fixed in vctrs.
Most helpful comment
Yes, this is a known issue and will (eventually) be fixed in vctrs.