Hi All, I am trying to convert the vector below to dates please assist I have tried to use information on the links you sent, but it is not working. X = c(201501, 201502, 201503, 201505, 201506, 201507, 201508, 201509, 201510, 201511, 201512, 201601, 201602, 201603, 201604, 201605, 201606) library(chron, zoo) Z = as.yearmon(X) # it is not working please assist Kind regards Peter Please Note: This email and its contents are subject to our email legal notice which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx [[alternative HTML version deleted]]
You can not convert numeric vectors directly to yearmon object. You must convert the X variable to character and add ?-? between year and month. Then as.yearmon function will work properly. Please, read help pages of ?as.character, ?strptime and ?as.yearmon. Example:> library(zoo) > aaa <- as.yearmon(c("2015-02?, ?2014-06")) > class(aaa)[1] "yearmon"> On 13 Jul 2016, at 16:25, Mangalani Peter Makananisa <pmakananisa at sars.gov.za> wrote: > > Hi All, > > I am trying to convert the vector below to dates please assist I have tried to use information on the links you sent, but it is not working. > > X = c(201501, 201502, 201503, 201505, 201506, 201507, 201508, 201509, 201510, 201511, 201512, 201601, 201602, 201603, 201604, 201605, 201606) > > library(chron, zoo) > Z = as.yearmon(X) # it is not working > > please assist > > Kind regards > Peter > > Please Note: This email and its contents are subject to our email legal notice which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
You need to look at the examples on the manual pages for ?yearmon and ?strptime:> Z <- as.yearmon(as.character(X), "%Y%m") > Z[1] "Jan 2015" "Feb 2015" "Mar 2015" "May 2015" "Jun 2015" "Jul 2015" "Aug 2015" [8] "Sep 2015" "Oct 2015" "Nov 2015" "Dec 2015" "Jan 2016" "Feb 2016" "Mar 2016" [15] "Apr 2016" "May 2016" "Jun 2016" Or> Z <- as.yearmon(round(X/100) + (X - round(X/100)*100 - 1)/12) > Z[1] "Jan 2015" "Feb 2015" "Mar 2015" "May 2015" "Jun 2015" "Jul 2015" "Aug 2015" [8] "Sep 2015" "Oct 2015" "Nov 2015" "Dec 2015" "Jan 2016" "Feb 2016" "Mar 2016" [15] "Apr 2016" "May 2016" "Jun 2016" ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Mangalani Peter Makananisa Sent: Wednesday, July 13, 2016 8:25 AM To: r-help at r-project.org Subject: [R] Dates in R (Year Month) Hi All, I am trying to convert the vector below to dates please assist I have tried to use information on the links you sent, but it is not working. X = c(201501, 201502, 201503, 201505, 201506, 201507, 201508, 201509, 201510, 201511, 201512, 201601, 201602, 201603, 201604, 201605, 201606) library(chron, zoo) Z = as.yearmon(X) # it is not working please assist Kind regards Peter Please Note: This email and its contents are subject to our email legal notice which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx [[alternative HTML version deleted]] ______________________________________________ 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.
Thanks. -----Original Message----- From: David L Carlson [mailto:dcarlson at tamu.edu] Sent: 13 July 2016 05:05 PM To: Mangalani Peter Makananisa; r-help at r-project.org Subject: RE: Dates in R (Year Month) You need to look at the examples on the manual pages for ?yearmon and ?strptime:> Z <- as.yearmon(as.character(X), "%Y%m") Z[1] "Jan 2015" "Feb 2015" "Mar 2015" "May 2015" "Jun 2015" "Jul 2015" "Aug 2015" [8] "Sep 2015" "Oct 2015" "Nov 2015" "Dec 2015" "Jan 2016" "Feb 2016" "Mar 2016" [15] "Apr 2016" "May 2016" "Jun 2016" Or> Z <- as.yearmon(round(X/100) + (X - round(X/100)*100 - 1)/12) Z[1] "Jan 2015" "Feb 2015" "Mar 2015" "May 2015" "Jun 2015" "Jul 2015" "Aug 2015" [8] "Sep 2015" "Oct 2015" "Nov 2015" "Dec 2015" "Jan 2016" "Feb 2016" "Mar 2016" [15] "Apr 2016" "May 2016" "Jun 2016" ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Mangalani Peter Makananisa Sent: Wednesday, July 13, 2016 8:25 AM To: r-help at r-project.org Subject: [R] Dates in R (Year Month) Hi All, I am trying to convert the vector below to dates please assist I have tried to use information on the links you sent, but it is not working. X = c(201501, 201502, 201503, 201505, 201506, 201507, 201508, 201509, 201510, 201511, 201512, 201601, 201602, 201603, 201604, 201605, 201606) library(chron, zoo) Z = as.yearmon(X) # it is not working please assist Kind regards Peter Please Note: This email and its contents are subject to our email legal notice which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx [[alternative HTML version deleted]] ______________________________________________ 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. Please Note: This email and its contents are subject to our email legal notice which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx [[alternative HTML version deleted]]