R向整列添加特定(不同)的时间量

Zac*_*aum 0 r dataframe posixct

我在R里有一张桌子:

start                duration
02/01/2012 20:00:00  5
05/01/2012 07:00:00  6
etc...               etc...
Run Code Online (Sandbox Code Playgroud)

我通过从Microsoft Excel导入一个如下所示的表来实现此目的:

date        time      duration
2012/02/01  20:00:00  5
etc...
Run Code Online (Sandbox Code Playgroud)

然后,我通过运行以下代码合并日期和时间列:

d.f <- within(d.f, { start=format(as.POSIXct(paste(date, time)), "%m/%d/%Y %H:%M:%S") })
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为'end'的第三列,它将计算为开始时间之后的小时数.我很确定我的时间是POSIXct向量.我已经看过如何操作一个日期时间对象,但是我怎么能为整个列做到这一点?

预期结果应如下所示:

start                duration  end
02/01/2012 20:00:00  5         02/02/2012 01:00:00
05/01/2012 07:00:00  6         05/01/2012 13:00:00
etc...               etc...    etc...
Run Code Online (Sandbox Code Playgroud)

Exp*_*teR 5

运用 lubridate

> library(lubridate)
> df$start <- mdy_hms(df$start)
> df$end <- df$start + hours(df$duration)
> df
#                start duration                 end
#1 2012-02-01 20:00:00        5 2012-02-02 01:00:00
#2 2012-05-01 07:00:00        6 2012-05-01 13:00:00
Run Code Online (Sandbox Code Playgroud)

数据

df <- structure(list(start = c("02/01/2012 20:00:00", "05/01/2012 07:00:00"
), duration = 5:6), .Names = c("start", "duration"), class = "data.frame", row.names = c(NA, 
-2L))
Run Code Online (Sandbox Code Playgroud)