Reputation: 671
I am attempting to convert several columns to dates if they follow the format of "days since origin" (here, if nchar(col)==5) and encountering an error:
Example data:
df <- data.frame(col1=c(44008, 440017, NA, 77), col2=c(43476, 43479, 77, NA))
> df
col1 col2
1 44008 43476
2 440017 43479
3 NA 77
4 77 NA
Attempt:
df %>%
mutate_at(vars(col1, col2), funs(as.numeric(as.character(.)))) %>%
mutate_at(vars(col1, col2),
funs(case_when((nchar(.)==5)~(as.Date(., origin = "1899-12-30")),TRUE~.)) )
Error message:
Error in as.Date.numeric(value) : 'origin' must be supplied
Desired:
col1 col2
1 2020-06-26 2019-01-11
2 3104-09-21 2019-01-14
3 <NA> 77
4 77 <NA>
Why am I getting an error if I provided the origin date? Thank you in advance.
Upvotes: 1
Views: 96
Reputation: 887691
If the intention is to convert only those elements with number of characters at least 5 to Date
class and keep the others as such. But, in the process, we cannot have a Date
class as numbers such as 77 can be either numeric or string. Here, we convert to string with as.character
library(dplyr) # v >= 1.0.0
df %>%
mutate(across(everything(),
~ case_when(nchar(.) >= 5
~ as.character(as.Date(., origin = '1899-12-30')),
TRUE ~ as.character(.))))
# col1 col2
#1 2020-06-26 2019-01-11
#2 3104-09-21 2019-01-14
#3 <NA> 77
#4 77 <NA>
Upvotes: 1