how can changing the date to special date?

hi, I have a date column in the data frame and another column in a data frame that have a special date from my country that many rows some date is NA because some month's in my country is 29 days and some other 31 days. know I want to convert these ‍‍NA rows to the correct date. how can do it?

    "date" = c("2017-02-14",
               "2018-05-21", 
               "2018-05-23",
               "2018-05-26",
               "2018-05-28"),
    "idate" = c("1395-11-26",
                "NA",
                "1397-03-02",
                "1397-03-05",
                "1397-03-07")
)

i get system location by Sys.timezone(location = TRUE) is Asia/Tehran

do you want to compute on them, or simply store and view them ?

I want to replace the correct date replace by NA. for example, in the up data frame, I want to replace 1397-02-31 instade of NA

so don't try to use Date datatype, use a character string, it can say whatever you want it to say....
no ?

I said anything .I want now up tibble change to NA to my special date .

library(tibble)
(t1 <- tibble(
  "date" = as.Date(c(
    "2017-02-14",
    "2018-05-21",
    "2018-05-23",
    "2018-05-26",
    "2018-05-28"
  )),
  "idate" = c(
    "1395-11-26",
    "0000-00-00", # <--- can be anything you want
    "1397-03-02",
    "1397-03-05",
    "1397-03-07"
  )
))

t1[[2, 2]]

t1[[2, 2]] <- "something else"

t1

t1[[2, 2]] <- "1397-13-13" # <- can be anything

t1

thanks but it's not true , because we don't have this date.
don't have any package to convert georgian date to islamic date?

There is a package ConvCalendar that was removed from CRAN due to lack of maintenance, but it can still be installed and used. I use renv



# library(renv)
# renv::install("cran/ConvCalendar")
library(ConvCalendar) 

gregorian<-as.Date("2018-05-21")
gregorian

(p <- as.OtherDate(gregorian,"persian"))


library(stringr)
s <- function(x){str_pad(x,width=2,pad="0")}
prnt_per <- function(x){
  paste(x$year,
        s(x$month),
        s(x$day),sep = "-")
}

prnt_per(p)
1397-02-31
1 Like

if (!require("lubridate")) install.packages("lubridate")
library(lubridate)

mutate(data = ymd(data))

ymd = year, month, day. You can pick the combination you want.

this will fail as westen calenders as supported by lubridate, dont have 31 days in february (and the issue was more that sas_008 didnt know that 2018-05-21 mapped to 1397-02-31 and needed help to calculate that fact.

1 Like

thanks @nirgrahamuk
if I want to use this way too many rows of the column of data frame how can do it?

You're right!

Sorry.

this is long but explicit, you can reduce it if you want..



# library(renv)
# renv::install("cran/ConvCalendar")
library(ConvCalendar) 

library(tidyverse)

(start_df <- enframe(seq.Date(from = as.Date("2018-01-01"),
                 to = as.Date("2018-12-31"),
                 by = "day"),value="gregorian_dt"))

(interim_df <- mutate(rowwise(start_df),
                      per_date_calc = list(as.OtherDate(gregorian_dt,"persian"))))



library(stringr)
s <- function(x){str_pad(x,width=2,pad="0")}
prnt_per <- function(x){
  paste(x$year,
        s(x$month),
        s(x$day),sep = "-")
}

(final_df <- mutate(rowwise(interim_df),
                    per_date_printed = prnt_per(per_date_calc)))

# a check
filter(final_df,
       gregorian_dt == '2018-05-21')

no I meant how I might apply it in a tibble and no for one date

I provide that in my latest example, you didnt try it or your would have seen...

    "date" = c("2017-02-14",
               "2018-05-21", 
               "2018-05-23",
               "2018-05-26",
               "2018-05-28",
               "2018-09-22",
               "2019-05-19",
               "2019-05-20",
               "2019-05-21",
               "2019-09-18",
               "2019-09-22"
    ),
    "idate" = c("1395-11-26",
                "NA",
                "1397-03-02",
                "1397-03-05",
                "1397-03-07",
                "NA",
                "NA",
                "NA",
                "NA",
                "1398-06-27",
                "NA")
)

for example for this tibble and another's tibble by many NA date.

start_df in my code is an example of the type of tibble you might start with....
take your own tibble, and mutate it as I do,...
Do you have a specific problem ? I thought you have experience with tidyverse and know how to mutate tibbles ?

in start_df we have from until to specific date but in data frame we don't know accurate date in start and end of data frame. know i want to underestand that we can use another method for it or no?

Saso says to Nir.. "I have numbers in a dataframe that I want to square ( but I wont share them) , how do I do it ?"

Nir says "like this

start_df <- enframe(seq.int(1,10,1))

(end_df <- mutate(start_df,
                 squared = value*value))

then Saso makes his own start df ...

 (start_df <- data.frame(value=c(9,1,100)))

Saso uses same code as Nir provided

 (end_df <- mutate(start_df,
                   squared = value*value))

do you understand that start_df is just a name that contains information, we can process that regardless of whats in it. if I put the 'wrong' things in mine, you can replace it with your own...

1 Like