我有一个包含以下内容的CSV文件:
ts1<-read.table(header = TRUE, sep=",", text="
start, end, value
1,26/11/2014 13:00,26/11/2014 20:00,decreasing
2,26/11/2014 20:00,27/11/2014 09:00,increasing ")
我想将上述dataframe
转移到dataframe
,其中每行time
列都会打开并填入值。时间差距从start
时间到end
时间 - 1(减去1)填写,如下所示:
date hour value
1 26/11/2014 13:00 decreasing
2 26/11/2014 14:00 decreasing
3 26/11/2014 15:00 decreasing
4 26/11/2014 16:00 decreasing
5 26/11/2014 17:00 decreasing
6 26/11/2014 18:00 decreasing
7 26/11/2014 19:00 decreasing
8 26/11/2014 20:00 increasing
9 26/11/2014 21:00 increasing
10 26/11/2014 22:00 increasing
11 26/11/2014 23:00 increasing
12 26/11/2014 00:00 increasing
13 26/11/2014 01:00 increasing
14 26/11/2014 02:00 increasing
15 26/11/2014 03:00 increasing
16 26/11/2014 04:00 increasing
17 26/11/2014 05:00 increasing
18 26/11/2014 06:00 increasing
19 26/11/2014 07:00 increasing
20 26/11/2014 08:00 increasing
我试着将日期与日期分开:
> t <- strftime(ts1$end, format="%H:%M:%S")
> t
[1] "00:00:00" "00:00:00"
答案 0 :(得分:1)
我们可以使用CURRENT_TIMESTAMP
。转换&#39; data.frame&#39;到&#39; data.table&#39; (data.table
),按行序列(setDT(ts1)
)分组,我们转换了&#39; start&#39;并且&#39;结束&#39;列到datetime类(使用1:nrow(ts1)
中的dmy_hm
),获取序列lubridate
&#39; 1小时&#39;,by
结果为预期格式,然后拆分按空格(format
),与“&#39;值”连接。栏目,删除&#39;&#39;列分配给tstrsplit
。最后,我们可以更改列名称(如果需要)。
NULL
答案 1 :(得分:1)
这是使用lubridate和plyr的解决方案。它处理数据的每一行以从开始到结束生成序列,并使用该值返回此序列。每行的结果合并为一个data.frame。如果您需要进一步处理结果,最好不要将日期时间分为日期和时间
library(plyr)
library(lubridate)
ts1$start <- dmy_hm(ts1$start)
ts1$end <- dmy_hm(ts1$end)
adply(.data = ts1, .margin = 1, .fun = function(x){
datetime <- seq(x$start, x$end, by = "hour")
#data.frame(datetime, value = x$value)"
data.frame(date = as.Date(datetime), time = format(datetime, "%H:%M"), value = x$value)
})[, -(1:2)]