Would you like to tell me how to fix my code? I want column D.LasrPV and D.LastCEMI as IDate, but below code give me errors..
tribble(
~SUBJID, ~D.LastPV, ~D.LastCEMI,
2610041, as.IDate('2022/05/23'), as.IDate('09/05/2022'),
2618012, as.IDate('2022/09/02'), as.IDate('09/02/2022'),
2641012, as.IDate('2022/07/25'), as.IDate('08/29/2022'))
Error gives me: Error in charToDate(x) : character string is not in a standard unambiguous format
CodePudding user response:
Testing your reprex line by line:
> as.IDate('2022/05/23')
[1] "2022-05-23"
> as.IDate('09/05/2022')
[1] "0009-05-20"
> as.IDate('2022/09/02')
[1] "2022-09-02"
> as.IDate('09/02/2022')
[1] "0009-02-20"
> as.IDate('2022/07/25')
[1] "2022-07-25"
> as.IDate('08/29/2022')
Error in charToDate(x) :
character string is not in a standard unambiguous format
as.IDate()
doesn't seem to be doing what you want.
Fix by specifying the date formats:
testframe <- tribble(
~SUBJID, ~D.LastPV, ~D.LastCEMI,
2610041, '2022/05/23', '09/05/2022',
2618012, '2022/09/02', '09/02/2022',
2641012, '2022/07/25', '08/29/2022')
testframe %>% mutate(
D.LastPV = as.IDate(D.LastPV, "%Y/%m/%d"),
D.LastCEMI = as.IDate(D.LastCEMI, "%m/%d/%Y")
)
gives:
># A tibble: 3 × 3
SUBJID D.LastPV D.LastCEMI
<dbl> <date> <date>
1 2610041 2022-05-23 2022-09-05
2 2618012 2022-09-02 2022-09-02
3 2641012 2022-07-25 2022-08-29