Why is coerce split double to integer in R and is there a workaround

You can build dates in R from numeric types, even fractional values. This is not very common, but it happens, for example, when averaging dates. Unfortunately, they seem to violate split

 > as.Date(-1, origin = "1970-01-01") [1] "1969-12-31" > as.Date(-1.0001, origin = "1970-01-01") [1] "1969-12-30" > split(as.Date(-1, origin = "1970-01-01"), 1)[[1]] [1] "1969-12-31" > split(as.Date(-1.0001, origin = "1970-01-01"), 1)[[1]] [1] "1969-12-31" #this is wrong > unclass(split(as.Date(-1, origin = "1970-01-01"), 1)[[1]]) [1] -1 > unclass(split(as.Date(-1.0001, origin = "1970-01-01"), 1)[[1]]) [1] -1 #this is "why" 

Thus, two dates that were different are equal to the split value. Do people agree that this is a mistake, or am I missing a deep reason? Any workarounds? Thanks

+6
source share
1 answer

For some reason, split.Date forces the input of Date into an integer:

 > split.Date function (x, f, drop = FALSE, ...) { y <- split.default(as.integer(x), f, drop = drop) for (i in seq_along(y)) class(y[[i]]) <- "Date" y } <bytecode: 0x2effb98> <environment: namespace:base> 

This is at least the ambiguity between the function and the documentation, because ?Date says: "the date must be an integer, but this does not apply in the internal representation." Some may consider this a mistake. I'm not sure.

You can avoid this by calling split.default directly.

 > split.default(as.Date(-1.0001, origin = "1970-01-01"), 1)[[1]] [1] "1969-12-30" 
+6
source

Source: https://habr.com/ru/post/980791/


All Articles