简体   繁体   English

R中的日期和时间总计

[英]Aggregate date and time in R

My data has a start and end time stamp such as this: 我的数据具有这样的开始和结束时间戳记:

200401010000 200401010030
200401010030 200401010100
200401010100 200401010130 and so on...

I'm trying to convert these fields into %YYYY%MM%DD%HH%MM format using lubridate and as.POSIXct but it I get only NAs. 我正在尝试使用lubridate和as.POSIXct将这些字段转换为%YYYY%MM%DD%HH%MM格式,但是我只能得到NA。 Any help will be appreciated. 任何帮助将不胜感激。 My goal is to aggregate the data for each month. 我的目标是汇总每个月的数据。 The code I've used so far is as follows: 到目前为止,我使用的代码如下:

start_time = as.POSIXct(dat$TIMESTAMP_START, format = "%YYYY%MM%DD %HH%MM",origin = "2004-01-01 00:00", tz="EDT")
stop_time = as.POSIXct(dat$TIMESTAMP_END, format = "%YYYY%MM%DD%HH%MM",origin = "2004-01-01 00:30", tz="EDT")
dat$interval <- interval(start_time, stop_time)

Two problems I can see: 我可以看到两个问题:

  1. If you're using lubridate already, you should probably use the function ymd_hm() , which is just cleaner IMO. 如果您已经在使用lubridate ,则应该使用ymd_hm()函数,该函数更干净。

  2. You can't apply that function to a vector (which I presume dat$TIMESTAMP_START and dat$TIMESTAMP_END are); 您不能将该函数应用于向量(我假设dat$TIMESTAMP_STARTdat$TIMESTAMP_END是); to do this, you can use: 为此,您可以使用:

     start_time <- sapply(dat$TIMESTAMP_START, ymd_hm()) end_time <- sapply(dat$TIMESTAMP_END, ymd_hm()) 

    That will apply the function to each item in your vector. 这会将功能应用于向量中的每个项目。

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

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