[R] Date and Time

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Mon Jul 18 06:38:41 CEST 2022


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

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:

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

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

You can see if it worked by adding 1 day to it:

> date_part+1
[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 using r-project.org> On Behalf Of Gregory Coats via
Sent: Sunday, July 17, 2022 11:47 PM
To: Gregory Coats via R-help <r-help using 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?
2022-01-01  7:28:10
2022-01-02  7:28:17
Greg Coats

More information about the R-help mailing list