在条件下添加上一行的值

时间:2017-04-18 08:35:50

标签: r conditional-statements

我有一个df data,如果因子相同,我想在新列中添加一个存在于前一列和行中的值。

以下是一个示例:

data <- structure(list(Id = c("a", "b", "b", "b", "a", "a", "b", "b", 
"a", "a"), duration.minutes = c(NA, 139L, 535L, 150L, NA, NA, 
145L, 545L, 144L, NA), event = structure(c(1L, 4L, 3L, 4L, 2L, 
1L, 4L, 3L, 4L, 2L), .Label = c("enter", "exit", "stop", "trip"
), class = "factor")), .Names = c("Id", "duration.minutes", "event"
), class = "data.frame", row.names = 265:274)

我想添加一个名为“duration.minutes.past”的新列,如下所示:

data <- structure(list(Id = c("a", "b", "b", "b", "a", "a", "b", "b", 
"a", "a"), duration.minutes = c(NA, 139L, 535L, 150L, NA, NA, 
145L, 545L, 144L, NA), event = structure(c(1L, 4L, 3L, 4L, 2L, 
1L, 4L, 3L, 4L, 2L), .Label = c("enter", "exit", "stop", "trip"
), class = "factor"), duration.minutes.past = c(NA, NA, 139, 
NA, NA, NA, NA, 145, NA, NA)), .Names = c("Id", "duration.minutes", 
"event", "duration.minutes.past"), row.names = 265:274, class = "data.frame")

正如您所看到的,我在同一duration.minutes.past的新专栏duration.minutes中添加了trip的{​​{1}}。如果Id不同或者不是停止,则Id的值为NA。

非常感谢帮助!

2 个答案:

答案 0 :(得分:2)

使用 Volume CY Volume LY 20152 88.868.719 49.123.12 20162 92.906.793 88.868.719 20172 105.029.725 92.906.793 Grand Total 186.687.824

的可能解决方案
dplyr

答案 1 :(得分:1)

我们可以使用data.table执行此操作。将'data.frame'转换为'data.table'(setDT(data)),按'Id'分组,我们使用lag创建'{1}}列'duration.minutes',然后将值更改为'NA',其中'event'不等于'stop'

shift

或者可以使用library(data.table) setDT(data)[, duration.minutes.past := shift(duration.minutes), Id][event != "stop", duration.minutes.past := NA][] data # Id duration.minutes event duration.minutes.past #1: a NA enter NA #2: b 139 trip NA #3: b 535 stop 139 #4: b 150 trip NA #5: a NA exit NA #6: a NA enter NA #7: b 145 trip NA #8: b 545 stop 145 #9: a 144 trip NA #10: a NA exit NA

使用base R来完成此操作
ave