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]]
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:>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 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.-- Sent from my phone. Please excuse my brevity.
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:> 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 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:> 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 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.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