[R] Convert string to date time

Sorkin, John j@ork|n @end|ng |rom @om@um@ry|@nd@edu
Wed Nov 27 01:53:27 CET 2024


I am reading a string that has the following form:

"2020-08-26_05:15:01"

I want to convert the string to a date-time variable. I tired:

x <- "2007-02-01_10:10:30"
x <- as.POSIXct(x,tz=Sys.timezone())
x

but this did not work; the time portion was ignored. I suspect the problem is the _ between the date and time, but I don't know how to account for this character in the string.

I hope someone can tell me how to read the string as a date time constant.

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





More information about the R-help mailing list