You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Coercion of a duration into a character and back does not work #1005
I understand that the basic purpose of the parser is different and that this behavior is expected. However, I just encountered a situation where this is problematic:
df<- tibble(x= duration(1, "years")) # x is a durationtmp<- tempfile(); write_csv(df, tmp) # write the df to filedf2<- read_csv(tmp) # x is now a character which can't be easily coerced
Now, I get that one could just convert a duration as a numeric before writing, via mutate(across(where(is.duration), as.numeric)) or the "old" way mutate_if(is.duration, as.numeric). But it seems like an unnecessary burden to the user, especially since this behavior is not, to the best of my knowledge, documented.
I will also file a bug report in readr. While I understand that readr can't be expected to know whether to coerce to numeric or character for every object class out there, I would have expected that classes from the tidyverse packages should be supported seamlessly. However, it seems that the "responsibility" of providing a parser for the duration class should be on lubridate.
Perhaps rather naively, it also seems to me that as.duration() should be able to handle the string format it produces. The information is still there, and the coercion could be achieved by reading the string and ignoring everything after s.
Right now, for character input, as.duration() calls as.period(), which calls parse_period(), which calls C_parse_period(). It's basically attempting to convert the duration column in string form to a period column and back.
Would it make sense to check if the string is in that non-numeric display duration format? For instance inside of as.duration() in case as.period() yields NA, or -- if that's too slow -- within C_parse_period().
Activity
tvatter commentedon Nov 17, 2021
Edit, I see that this isn't a new problem with
readr:surdina commentedon Jan 11, 2022
I just came across the same thing!
Perhaps rather naively, it also seems to me that
as.duration()should be able to handle the string format it produces. The information is still there, and the coercion could be achieved by reading the string and ignoring everything afters.Right now, for character input,
as.duration()callsas.period(), which callsparse_period(), which callsC_parse_period(). It's basically attempting to convert the duration column in string form to a period column and back.Would it make sense to check if the string is in that non-numeric display duration format? For instance inside of
as.duration()in caseas.period()yieldsNA, or -- if that's too slow -- withinC_parse_period().Edit: Easy ad-hoc workaround for now:
Applied to the example above:
vspinu commentedon Jan 12, 2022
Thanks or bringing it up.
(time/R-lubridate) Updated 1.7.10 to 1.9.2
(time/R-lubridate) Updated 1.7.10 to 1.9.2
(time/R-lubridate) Updated 1.7.10 to 1.9.2
(time/R-lubridate) Updated 1.7.10 to 1.9.2