简体   繁体   English

如何在 R 中将“Year”、“Day of the Year”和“Hour”列组合到 DateTime?

[英]How to Combine “Year”, “Day of the Year”, and “Hour” columns to DateTime in R?

I have long dataframe as below (Note: DoY: Day of the Year, Hour: Hour of the Day (eg, Hour =0.5 means 12:30 AM):我有很长的 dataframe 如下(注:DoY:一年中的一天,小时:一天中的小时(例如,小时 = 0.5 表示上午 12:30):

Year   DoY   Hour
2016   126   0.5
2016   126   1
2016   126   1.5
 -      -     -
2016   127   0
2016   127   0.5
 -      -     -
2018   300   23.5
 -      -     -

I am trying to combine these columns to single DateTime column (eg 2016-05-05 12:30 AM, 2016-05-05 1:00 AM, etc.).我正在尝试将这些列合并为单个 DateTime 列(例如 2016-05-05 12:30 AM、2016-05-05 1:00 AM 等)。 I tried following code:我尝试了以下代码:

x <- as.numeric(df$Hour)
x.m <- paste(floor(x), round((x-floor(x))*60), sep=":")
df$HourMinute <- x.m 
df$DateTime <- strptime(paste0(df$Year, df$DoY, df$HourMinute), format = "%Y%j%H:%M")

Above code results into some strange output.上面的代码导致一些奇怪的 output。 After a Year end, It outputs NA values.一年结束后,它输出 NA 值。 How to create the desired output column?如何创建所需的 output 列?

You could use lubridate :你可以使用lubridate

library(lubridate)

df$date <- make_datetime(year = df$Year, min = round(df$Hour*60)) + days(df$DoY-1)

df

#>   Year DoY Hour                date
#> 1 2016 126  0.5 2016-05-05 00:30:00
#> 2 2016 126  1.0 2016-05-05 01:00:00
#> 3 2016 126  1.5 2016-05-05 01:30:00

Data:数据:

df <- structure(list(Year = c(2016L, 2016L, 2016L), DoY = c(126L, 126L, 126L), 
                     Hour = c(0.5, 1, 1.5)),
                     class = "data.frame", row.names = c(NA,-3L))

Here is a base R way.这是一个基本的 R 方式。

fun <- function(DF){
  d <- with(DF, paste(Year, DoY))
  d <- as.Date(d, "%Y %j")
  hm <- DF[["Hour"]]*60
  d <- paste(d, paste(hm %/% 60, hm %% 60, 0, sep = ":"))
  d <- as.POSIXct(d, "%Y-%m-%d %H:%M:%S")
  d
}

fun(df)
#[1] "2016-05-05 00:30:00" "2016-05-05 01:00:00"
#[3] "2016-05-05 01:30:00"

This result can be assigned to a new column in the usual way.可以按通常的方式将此结果分配给新列。

df$DateTime <- fun(df)

Data数据

df <- read.table(text = "
Year   DoY   Hour
2016   126   0.5
2016   126   1
2016   126   1.5
", header = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM