R:当日出/日落时间改变时,将日/夜列添加到数据框

时间:2016-04-28 13:04:23

标签: r datetime time

我有一个这样的数据集超过23天:

        temp1   DateTime
      1   448   2016-03-24   21:14:00
      2   448   2016-03-24   21:14:01
      3   448   2016-03-24   21:14:02
      4   448   2016-03-24   21:14:03
      5   448   2016-03-24   21:14:04
      6   448   2016-03-24   21:14:05
1930724   500   2016-04-15   13:46:21
1930725   500   2016-04-15   13:46:22
1930726   500   2016-04-15   13:46:23
1930727   500   2016-04-15   13:46:24
1930728   500   2016-04-15   13:46:25
1930729   500   2016-04-15   13:46:26

我现在想要添加一个新列,为每个day写入DateTime - 值大于sunrise time且小于sunset time,所以我得到:

      temp1   DateTime                 dayNight
      1   448   2016-03-24   21:14:00  night
      2   448   2016-03-24   21:14:01  night
      3   448   2016-03-24   21:14:02  night
      4   448   2016-03-24   21:14:03  night
      5   448   2016-03-24   21:14:04  night
      6   448   2016-03-24   21:14:05  night
1930724   500   2016-04-15   13:46:21  day
1930725   500   2016-04-15   13:46:22  day
1930726   500   2016-04-15   13:46:23  day
1930727   500   2016-04-15   13:46:24  day
1930728   500   2016-04-15   13:46:25  day
1930729   500   2016-04-15   13:46:26  day

我从函数中得到"2016-04-15 06:40:37 UTC"的日出和日落时间:

sunRise <- function(x){
sunrise.set(lat, long, x, timezone = "UTC", num.days = 1)[1,1]
}

sunSet <- function(x){
sunrise.set(lat, long, x, timezone = "UTC", num.days = 1)[1,2]
}

包含sunrise.set() - 来自包StreamMetabolism的{​​{1}}和lat <- 28.39675

现在的问题是那些太阳时代在数据框架的23天内发生了变化。

根据每一天的特定日出/设置时间,我有办法制作long <- -16.567131列吗?

有没有办法将每天的日出/设置时间作为一列添加到数据框?

1 个答案:

答案 0 :(得分:2)

这是一种方法(基于我的评论)并编制了我可以提出的数据:

library(dplyr)

# You don't need this line if you already have DateTime in proper format
df$DateTime <- as.POSIXct(df$DateTime)

# Add a date column (with whatever timezone you want)
df$date <- as.Date(df$DateTime, tz = 'EST')

# Following generates the sunrise and sunset times for the two example dates
sunRise <- c(as.POSIXct('2016-04-15 06:40:37'), as.POSIXct('2016-03-24 06:55:00'))
sunSet <- c(as.POSIXct('2016-04-15 18:40:37'), as.POSIXct('2016-03-24 18:25:00'))
sun <- data.frame(date = as.Date(sunRise, tz = 'EST'), sunRise = sunRise, sunSet = sunSet)
sun
        date             sunRise              sunSet
1 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37
2 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00

# Join the two tables and compute night/day
df <- inner_join(df, sun)
df$dayNight <- ifelse(df$DateTime > df$sunRise & df$DateTime < df$sunSet, 'day', 'night')

输出如下:

df
   temp1            DateTime       date             sunRise              sunSet dayNight
1    448 2016-03-24 21:14:00 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00    night
2    448 2016-03-24 21:14:01 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00    night
3    448 2016-03-24 21:14:02 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00    night
4    448 2016-03-24 21:14:03 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00    night
5    448 2016-03-24 21:14:04 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00    night
6    448 2016-03-24 21:14:05 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00    night
7    500 2016-04-15 13:46:21 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37      day
8    500 2016-04-15 13:46:22 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37      day
9    500 2016-04-15 13:46:23 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37      day
10   500 2016-04-15 13:46:24 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37      day
11   500 2016-04-15 13:46:25 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37      day
12   500 2016-04-15 13:46:26 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37      day

当然,您可以删除不必要的列并保留您想要的内容,如下所示:

df[, -c(3, 4, 5)]
   temp1            DateTime dayNight
1    448 2016-03-24 21:14:00    night
2    448 2016-03-24 21:14:01    night
3    448 2016-03-24 21:14:02    night
4    448 2016-03-24 21:14:03    night
5    448 2016-03-24 21:14:04    night
6    448 2016-03-24 21:14:05    night
7    500 2016-04-15 13:46:21      day
8    500 2016-04-15 13:46:22      day
9    500 2016-04-15 13:46:23      day
10   500 2016-04-15 13:46:24      day
11   500 2016-04-15 13:46:25      day
12   500 2016-04-15 13:46:26      day