Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I just want to convert the Persian date to the Gregorian using the ConvCalendar library. The character vector that I have used is as follows:

str(df$death_date)

 chr [1:286] NA NA NA NA "1399/03/12" NA NA NA NA NA NA NA NA NA "1399/03/25" NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA ...

death <- OtherDate(day=substr(df$death_date,9,10),

                 month=substr(df$death_date,6,7),

                 year=substr(df$death_date,1,4),

                 calendar="persian")

While converting the death into the Gregorian using as.Date(death), I am getting the following error:

Error in as.Date.OtherDate(death) : NAs in foreign function call (arg 4)

1 Answer

0 votes
by (108k points)

I think you have not installed the ConvCalendar.If it is connected to the source, then the following code will work:

library(ConvCalendar)

y <- x[!is.na(x)]

y <- as.POSIXlt(y, format = '%Y/%m/%d', origin = '1970-01-01')

pers <- OtherDate(day=y$mday, month=y$mon+1, year=y$year, calendar="persian")

as.Date(pers)

#[1] "0120-06-02" "0120-06-15"

Data:

x <- scan(what = character(), text = '

NA NA NA NA "1399/03/12" NA NA NA NA NA NA NA NA NA "1399/03/25" NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

')

If you are a beginner and want to learn more about R then do check out the R programming course.

Browse Categories

...