[R] Date and Time

Daniel Nordlund djnord|und @end|ng |rom gm@||@com
Mon Jul 18 06:50:44 CEST 2022


On 7/17/2022 8:47 PM, Gregory Coats via R-help wrote:
> For the year from 2022-01-01 to 2022-12-31, I computed the sunrise and sunset times for Reston, Virginia, USA. I am seeking the syntax to direct R to read in these dates and times, and then graphical plot the sunrise and sunset times for each day in the year 2022. How do I tell R that I store the Sunrise Date and Time like this?
> YYYY-mm-dd HH:MM:SS
> 2022-01-01  7:28:10
> 2022-01-02  7:28:17
> Greg Coats
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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.

Here is one way using the strptime function,

 > in_dt_str <- '2022-01-01  7:28:10'
 > dt <- strptime(in_dt_str,'%Y-%m-%d %H:%M:%S')

if the date and time are stored in different variables then paste them 
together before converting

 > in_dt <- '2022-01-01'
 > in_tm <- '7:28:10'
 > dtm <- strptime(paste(in_dt, in_tm, sep=' '),'%Y-%m-%d %H:%M:%S')

Hope this is helpful,

Dan

-- 
Daniel Nordlund
Port Townsend, WA  USA


-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the R-help mailing list