Skip to content

Date and Time

5 messages · Gregory Coats, Jeff Newmiller, @vi@e@gross m@iii@g oii gm@ii@com +2 more

#
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
#
Maybe this [1] will help? Or just read ?strptime...

You can also calculate sunrise/sunset (crepuscule) using maptools, but you need to be careful with timezones.

[1] https://jdnewmil.github.io/time-2018-10/MoreDatetimeHowto.html
On July 17, 2022 8:47:19 PM PDT, Gregory Coats via R-help <r-help at r-project.org> wrote:

  
    
#
Greg,

Strictly speaking, you don't tell R and keep it a secret!

More seriously, are you reading in your data as text of a sort in a file, or
as part of a CSV file or Spreadsheet or what?

What do you plan on graphing the data with? It sounds like you likely need
to make a data.frame or tibble that has at least two columns with one being
a DATE of some kind so you can plot the date, versus another being a kind of
TIME. 

There are many ways to record time in R as dates or date+time or duration
and you need to pick some that will work with the ploting program such as
base R or ggplot. 

You say your dates look like this:

YYYY-mm-dd HH:MM:SS
2022-01-01  7:28:10

It is fairly easy to split that into a date STRING in one variable and a
time string in another.

Then you need to convert a vector of each (perhaps in a data.frame) into
some objects of a type that hold the data properly.  You can use standard R
functions or various packages. If your format is already compatible, so
this:

date_part <- as.Date("2022-01-01")

You can see if it worked by adding 1 day to it:
[1] "2022-01-02"

If your format is something specific, you can pass templates to guide some
function in parsing it.

Similarly you can find R functions that store a time and of course more
advanced ones that store both in several ways. But for graphing what you
want as a sort of sinusoidal function, you would need to extract the time
and date from it.

If you search online, you can find lots of tutorials and discussions as this
is a very common thing. I don't mean sunsets, but measurements versus dates.


-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Gregory Coats via
R-help
Sent: Sunday, July 17, 2022 11:47 PM
To: Gregory Coats via R-help <r-help at r-project.org>
Subject: [R] Date and Time

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
#
On 7/17/2022 8:47 PM, Gregory Coats via R-help wrote:
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
#
An alternative is the lubridate() package if you use tidyverse.

Check the data for daylight savings time. Twice per year Virginia changes time where in the Fall clocks are shifted back an hour and in the Spring they jump forwards an hour.

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Jeff Newmiller
Sent: Monday, July 18, 2022 12:35 AM
To: Greg Comcast Coats <gregcoats at me.com>; Gregory Coats via R-help <r-help at r-project.org>
Subject: Re: [R] Date and Time

[External Email]

Maybe this [1] will help? Or just read ?strptime...

You can also calculate sunrise/sunset (crepuscule) using maptools, but you need to be careful with timezones.

[1] https://urldefense.proofpoint.com/v2/url?u=https-3A__jdnewmil.github.io_time-2D2018-2D10_MoreDatetimeHowto.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3XGPiP&s=O5VruA8vvqyhQngUZ3fuZtnZiYIm9XEscpzxcGKX9AQ&e=
On July 17, 2022 8:47:19 PM PDT, Gregory Coats via R-help <r-help at r-project.org> wrote:
--
Sent from my phone. Please excuse my brevity.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3XGPiP&s=7XAM9jGAgm-2KbQG5x3oKKxrvNESM5DAvl_O3YxSU3Y&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3XGPiP&s=MwaLmYJXfAwQDsHSzq8Atm-QC2YpJYie_L5oNkAJiVg&e=
and provide commented, minimal, self-contained, reproducible code.