[R] Using R lines() to show sunrise and sunset

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Tue Jul 19 03:43:21 CEST 2022


Your description is insufficiently precise for us to do more than guess at
what you have. I shall assume your data consists of text strings as shown.
Presumably, you can remove the inputs and collect the outputs into a data
frame, e.g.

dat <- read.table( text = "
2022-01-01 07:26:45 2022-01-01 16:57:07
2022-01-02 07:26:52 2022-01-02 16:57:56"
)[,-3]
## there are 4 text columns. The [,-3] removes the replicated date.

Then, as Jeff suggested,

names(dat) <- c('Date','Sunrise', 'Sunset')
dat
## gives:
          Date  Sunrise   Sunset
1 2022-01-01 07:26:45 16:57:07
2 2022-01-02 07:26:52 16:57:56

But, again, as you have not described your data structure in sufficient
detail (at least for me), this is just a guess.

Once you have the data in this format, you can extract what you need and
maybe use strptime() to convert to appropriate numeric formats. Or maybe
simply use strplit():

> strsplit(dat$Sunrise, split = ":")
[[1]]
[1] "07" "26" "45"

[[2]]
[1] "07" "26" "52"

Note: as.numeric() converts number strings to numbers:
> as.numeric("45")
[1] 45

Again, just a guess, and therefore perhaps useless.

-- Bert


On Mon, Jul 18, 2022 at 4:51 PM Gregory Coats via R-help <
r-help using r-project.org> wrote:

> I compiled a program on my Apple MacBook that takes as inputs
>         Year and Month and Day
>         Latitude and Longitude
> And then computes these two outputs
> Sunrise Year-Month-Day Hour:Minute:Second
> Sunset  Year-Month-Day Hour:Minute:Second
> It automatically handles Daylight Savings Time.
> A typical input, followed by the automatically computed outputs looks
> likes this.
>
> ./sunrise_05 2022 01 1 38.8586314239524 77.0512533684194
> 2022-01-01 07:26:45 2022-01-01 16:57:07
> ./sunrise_05 2022 01 2 38.8586314239524 77.0512533684194
> 2022-01-02 07:26:52 2022-01-02 16:57:56
>
> I want to use R’s lines() command to show the sunrise and sunset times for
> the year 2012. How do I tell R that the first computed output is sunrise,
> and the second computer output is sunset?
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list