Hi All, I wanted to to convert character date mm/dd/yy to YYYY-mm-dd The sample data and my attempt is shown below gs <-read.table(text="ID date A1 09/27/03 A2 05/27/16 A3 01/25/13 A4 09/27/19",header=TRUE,stringsAsFactors=F) Desired output ID date d1 A1 09/27/03 2003-09-27 A2 05/27/16 2016-05-27 A3 01/25/13 2012-04-25 A4 09/27/19 2019-09-27 I used this gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d") but I got NA's. How do I get my desired result? Thank you.
Try putting / instead of - in your format, to match the data. On Tue, Dec 17, 2019 at 5:52 PM Val <valkremk at gmail.com> wrote:> > Hi All, > > I wanted to to convert character date mm/dd/yy to YYYY-mm-dd > The sample data and my attempt is shown below > > gs <-read.table(text="ID date > A1 09/27/03 > A2 05/27/16 > A3 01/25/13 > A4 09/27/19",header=TRUE,stringsAsFactors=F) > > Desired output > ID date d1 > A1 09/27/03 2003-09-27 > A2 05/27/16 2016-05-27 > A3 01/25/13 2012-04-25 > A4 09/27/19 2019-09-27 > > I used this > gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d") > > but I got NA's. > > How do I get my desired result? > Thank you. > > ______________________________________________ > 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.
...and switch the order, and use %y for 2-digit years.> On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <malone at malonequantitative.com> wrote: > > Try putting / instead of - in your format, to match the data. > > On Tue, Dec 17, 2019 at 5:52 PM Val <valkremk at gmail.com> wrote: >> >> Hi All, >> >> I wanted to to convert character date mm/dd/yy to YYYY-mm-dd >> The sample data and my attempt is shown below >> >> gs <-read.table(text="ID date >> A1 09/27/03 >> A2 05/27/16 >> A3 01/25/13 >> A4 09/27/19",header=TRUE,stringsAsFactors=F) >> >> Desired output >> ID date d1 >> A1 09/27/03 2003-09-27 >> A2 05/27/16 2016-05-27 >> A3 01/25/13 2012-04-25 >> A4 09/27/19 2019-09-27 >> >> I used this >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d") >> >> but I got NA's. >> >> How do I get my desired result? >> Thank you. >> >> ______________________________________________ >> 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Val Another all-base R solution: as.Date(strptime(gs$date,format="%m/%d/%y")) or if you want to add a time field later as.POSIXct(strptime(gs$date,format="%m/%d/%y"))) since strptime produces a list version of the date: class is POSIXt and the subclass is POSIXlt, that can be convenient for extracting elements like month, day of the year, minute, etc. The actual date/time information is returned as the number of seconds since 1970-01-01 00:00:00 UTC but the representation is as in your gs.d1 below. It can be a bit tricky when daylight savings time comes into play but that's for another post. David On 12/17/2019 3:51 PM, Val wrote: Hi All, I wanted to to convert character date mm/dd/yy to YYYY-mm-dd The sample data and my attempt is shown below gs <-read.table(text="ID date A1 09/27/03 A2 05/27/16 A3 01/25/13 A4 09/27/19",header=TRUE,stringsAsFactors=F) Desired output ID date d1 A1 09/27/03 2003-09-27 A2 05/27/16 2016-05-27 A3 01/25/13 2012-04-25 A4 09/27/19 2019-09-27 I used this gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d") but I got NA's. How do I get my desired result? Thank you. ______________________________________________ R-help at r-project.org<mailto: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. -- David K Stevens, PhD, PE Environmental Engineering Division Civil and Environmental Engineering Utah State University 8200 Old Main Hill Logan, UT 83200-8200 (435) 797-3229 david.stevens at usu.edu<mailto:david.stevens at usu.edu> [[alternative HTML version deleted]]