Sorkin, John
2023-Dec-07 16:21 UTC
[R] Convert character date time to R date-time variable.
Colleagues, I have a matrix of character data that represents date and time. The format of each element of the matrix is "2020-09-17_00:00:00" How can I convert the elements into a valid R date-time constant? Thank you, John John David Sorkin M.D., Ph.D. Professor of Medicine, University of Maryland School of Medicine; Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center;? PI?Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; Senior Statistician University of Maryland Center for Vascular Research; Division of Gerontology and Paliative Care, 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 Cell phone 443-418-5382
Ebert,Timothy Aaron
2023-Dec-07 16:29 UTC
[R] Convert character date time to R date-time variable.
Look at the lubridate package in R. Regards, Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Sorkin, John Sent: Thursday, December 7, 2023 11:22 AM To: r-help at r-project.org (r-help at r-project.org) <r-help at r-project.org> Subject: [R] Convert character date time to R date-time variable. [External Email] Colleagues, I have a matrix of character data that represents date and time. The format of each element of the matrix is "2020-09-17_00:00:00" How can I convert the elements into a valid R date-time constant? Thank you, John John David Sorkin M.D., Ph.D. Professor of Medicine, University of Maryland School of Medicine; Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center; PI Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; Senior Statistician University of Maryland Center for Vascular Research; Division of Gerontology and Paliative Care, 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 Cell phone 443-418-5382 ______________________________________________ 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.
Rui Barradas
2023-Dec-07 16:30 UTC
[R] Convert character date time to R date-time variable.
?s 16:21 de 07/12/2023, Sorkin, John escreveu:> Colleagues, > > I have a matrix of character data that represents date and time. The format of each element of the matrix is > "2020-09-17_00:00:00" > How can I convert the elements into a valid R date-time constant? > > Thank you, > John > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine, University of Maryland School of Medicine; > > Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center; > > PI?Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; > > Senior Statistician University of Maryland Center for Vascular Research; > > Division of Gerontology and Paliative Care, > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > Cell phone 443-418-5382 > > > > ______________________________________________ > 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.Hello, Coerce with ?as.POSIXct Don't forget the underscore in the format. as.POSIXct("2020-09-17_00:00:00", format = "%Y-%m-%d_%H:%M:%S") Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Dirk Eddelbuettel
2023-Dec-07 16:59 UTC
[R] Convert character date time to R date-time variable.
`anytime` was written for this: > anytime::anytime("2020-09-17_00:00:00") [1] "2020-09-17 CDT" > class(anytime::anytime("2020-09-17_00:00:00")) [1] "POSIXct" "POSIXt" > Dirk -- dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
David Winsemius
2023-Dec-08 21:30 UTC
[R] Convert character date time to R date-time variable.
On 12/7/23 08:21, Sorkin, John wrote:> Colleagues, > > I have a matrix of character data that represents date and time. The format of each element of the matrix is > "2020-09-17_00:00:00" > How can I convert the elements into a valid R date-time constant?You will not be able to store these datetime values in an R matrix, at least as class POSIXct. You could with class POSIXlt, but I've not seen it used before but it does appear possible since matrices can contain lists. R matrices do no provide the capability to assign attributes, so only atomic types and lists can be elements. If you wanted to maintain the same structure, your first step might be to coerce to a data.frame and then proceed, or to first construct a vector and then use as the first argument to `matrix`. Best; David> > Thank you, > John > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine, University of Maryland School of Medicine; > > Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center; > > PI?Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; > > Senior Statistician University of Maryland Center for Vascular Research; > > Division of Gerontology and Paliative Care, > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > Cell phone 443-418-5382 > > > > ______________________________________________ > 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.