How to Create Date and Time Data for Years Only

Asked 2 years ago, Updated 2 years ago, 37 views

>as.Date("December 25, 2017")
[1] "2017-12-25"
>as.Date ("2017/12")
Error in charToDate(x): 
   The string is not formatted without standard ambiguity. 
>

I would like to create the date and time data for the year and month above.
Please tell me how to do it.

r

2022-09-30 19:34

1 Answers

Date is a day-by-day type as the Japanese language suggests, so it is not appropriate to express the month-by-month type.

The first day of every month is a day, so it is necessary to devise ways to represent the year on the first day of every month.

For example, using the seq function,

yyyymm<-seq(as.Date("January 1, 2018"), as.Date("December 31, 2018"), "months")

"2018-01-01", "2018-02-01", "2018-03-01", "2018-04-01", "2018-05-01", "2018-06-01", "2018-11-01", "2018-12-01", and create a vector called
We will treat them as data (date and time data) for the year of January 2018 and February 2018...2018/12.

===
Of course, I think it's okay to define the year/month class (in this case, YearMonth class) by yourself.If you look at the Date class, it won't be that difficult.

as.YearMonth("2017/12")
[1] "2017-12"


2022-09-30 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.