R-Help
Trying to convert a Gregorian calendar dataset to a Persian calendar
dataset. But I end up with a list and not sure what to do. For example ...
dates <- c("2017-10-1","2017-10-2","2017-10-3")
myData <- data.frame(dates)
myData$dates <- as.Date(myData$dates, format =
"%Y-%m-%d")> myData
dates
1 2017-10-01
2 2017-10-02
3 2017-10-03
library(ConvCalendar)
p.dates <- as.OtherDate(myData$dates,"persian")
# after convering I get the following list> p.dates
$day
[1] 9 10 11
$month
[1] 7 7 7
$year
[1] 1396 1396 1396
attr(,"row.names")
[1] 1 2 3
attr(,"class")
[1] "OtherDate"
attr(,"calendar")
[1] "persian"
How do I take that, to end up with
dates p_dates
1 2017-10-01 1396-7-9
2 2017-10-02 1396-7-10
3 2017-10-03 1396-7-11
Jeff Reichman
Penn State
How about> p_dates <- paste0(p.dates[[3]], "-", p.dates[[2]], "-", p.dates[[1]]) > myData$p_dates <- p_dates > print(myData, right=FALSE)dates p_dates 1 2017-10-01 1396-7-9 2 2017-10-02 1396-7-10 3 2017-10-03 1396-7-11> str(myData)'data.frame': 3 obs. of 2 variables: $ dates : Date, format: "2017-10-01" "2017-10-02" ... $ p_dates: chr "1396-7-9" "1396-7-10" "1396-7-11" But p_dates is a character field so it will not work with numeric expressions:> with(myData, dates[3] - dates[1])Time difference of 2 days # But> with(myData, p_dates[3] - p_dates[1])Error in p_dates[3] - p_dates[1] : non-numeric argument to binary operator ----------------------------- David L. Carlson Department of Anthropology Texas A&M University -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff Reichman Sent: Wednesday, November 8, 2017 5:05 PM To: r-help at r-project.org Subject: [R] Help Converting Calendars R-Help Trying to convert a Gregorian calendar dataset to a Persian calendar dataset. But I end up with a list and not sure what to do. For example ... dates <- c("2017-10-1","2017-10-2","2017-10-3") myData <- data.frame(dates) myData$dates <- as.Date(myData$dates, format = "%Y-%m-%d")> myDatadates 1 2017-10-01 2 2017-10-02 3 2017-10-03 library(ConvCalendar) p.dates <- as.OtherDate(myData$dates,"persian") # after convering I get the following list> p.dates$day [1] 9 10 11 $month [1] 7 7 7 $year [1] 1396 1396 1396 attr(,"row.names") [1] 1 2 3 attr(,"class") [1] "OtherDate" attr(,"calendar") [1] "persian" How do I take that, to end up with dates p_dates 1 2017-10-01 1396-7-9 2 2017-10-02 1396-7-10 3 2017-10-03 1396-7-11 Jeff Reichman Penn State ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Or if you want a slightly prettier output:
formatDate<-function(x) {
return(paste(x$year,formatC(x$month,width=2,flag=0),
formatC(x$day,width=2,flag=0),sep="-"))
}
formatDate(p.dates)
Jim
On Thu, Nov 9, 2017 at 10:32 AM, David L Carlson <dcarlson at tamu.edu>
wrote:> How about
>
>> p_dates <- paste0(p.dates[[3]], "-", p.dates[[2]],
"-", p.dates[[1]])
>> myData$p_dates <- p_dates
>> print(myData, right=FALSE)
> dates p_dates
> 1 2017-10-01 1396-7-9
> 2 2017-10-02 1396-7-10
> 3 2017-10-03 1396-7-11
>> str(myData)
> 'data.frame': 3 obs. of 2 variables:
> $ dates : Date, format: "2017-10-01" "2017-10-02" ...
> $ p_dates: chr "1396-7-9" "1396-7-10"
"1396-7-11"
>
> But p_dates is a character field so it will not work with numeric
expressions:
>
>> with(myData, dates[3] - dates[1])
> Time difference of 2 days
> # But
>> with(myData, p_dates[3] - p_dates[1])
> Error in p_dates[3] - p_dates[1] :
> non-numeric argument to binary operator
>
> -----------------------------
> David L. Carlson
> Department of Anthropology
> Texas A&M University
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff
Reichman
> Sent: Wednesday, November 8, 2017 5:05 PM
> To: r-help at r-project.org
> Subject: [R] Help Converting Calendars
>
> R-Help
>
> Trying to convert a Gregorian calendar dataset to a Persian calendar
> dataset. But I end up with a list and not sure what to do. For example
...
>
> dates <-
c("2017-10-1","2017-10-2","2017-10-3")
> myData <- data.frame(dates)
> myData$dates <- as.Date(myData$dates, format = "%Y-%m-%d")
>> myData
> dates
> 1 2017-10-01
> 2 2017-10-02
> 3 2017-10-03
> library(ConvCalendar)
> p.dates <- as.OtherDate(myData$dates,"persian")
> # after convering I get the following list
>> p.dates
> $day
> [1] 9 10 11
>
> $month
> [1] 7 7 7
>
> $year
> [1] 1396 1396 1396
>
> attr(,"row.names")
> [1] 1 2 3
> attr(,"class")
> [1] "OtherDate"
> attr(,"calendar")
> [1] "persian"
>
> How do I take that, to end up with
>
> dates p_dates
> 1 2017-10-01 1396-7-9
> 2 2017-10-02 1396-7-10
> 3 2017-10-03 1396-7-11
>
> Jeff Reichman
> Penn State
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.