首页 > 解决方案 > 添加条件公式列

问题描述

我有一个带有propertyIDfromLocalTime列的数据框。在 R 中,我想创建followup在每一行中执行此逻辑的列:

如果 propertyID row #235364=propertyID row #235363,则返回 fromLocalTime (row #235364 - fromLocalTime row #235363),否则返回 0。

(注意后续栏的格式为数字天数)

示例数据输出

> dput(head(df))
structure(list(propertyID = c(924561.18, 924561.18, 924561.18, 
924601.14, 924601.14, 924647.76), fromLocalTime = structure(c(1570808280, 
1571231640, 1571246760, 1570799580, 1571231400, 1571160060), class = c("POSIXct", 
"POSIXt"), tzone = ""), followup = c(NA, 4.9, 0.175, NA, 
0.208246527777778, NA)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -6L), groups = structure(list(
    propertyID = c(924561.18, 924601.14, 924647.76), .rows = list(
        1:3, 4:5, 6L)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

> data.frame(df)
   propertyID       fromLocalTime       followup
1    924561.2 2019-10-11 11:38:00             NA
2    924561.2 2019-10-16 09:14:00      4.9000000
3    924561.2 2019-10-16 13:26:00      0.1750000
4    924601.1 2019-10-11 09:13:00             NA
5    924601.1 2019-10-16 09:10:00      0.2082465
6    924647.8 2019-10-15 13:21:00             NA
7    924654.4 2019-10-15 09:08:00             NA
8    924677.7 2019-09-20 14:25:00             NA
9    924677.7 2019-09-23 11:40:00      0.1202257
10   924724.3 2019-10-17 13:10:00             NA
11   925936.5 2019-10-15 12:06:00             NA
12   925936.5 2019-10-16 08:03:00      0.8312500
13   925939.8 2019-10-15 11:11:00             NA
14   926529.2 2019-10-17 11:04:00             NA

因此,我的预期输出是:

> data.frame(df)
   propertyID       fromLocalTime       followup
1    924561.2 2019-10-11 11:38:00             NA
2    924561.2 2019-10-16 09:14:00      4.9000000
3    924561.2 2019-10-16 13:26:00      0.1750000
4    924601.1 2019-10-11 09:13:00             NA
5    924601.1 2019-10-16 09:10:00      4.997917
6    924647.8 2019-10-15 13:21:00             NA
7    924654.4 2019-10-15 09:08:00             NA
8    924677.7 2019-09-20 14:25:00             NA
9    924677.7 2019-09-23 11:40:00      2.885417
10   924724.3 2019-10-17 13:10:00             NA
11   925936.5 2019-10-15 12:06:00             NA
12   925936.5 2019-10-16 08:03:00      0.8312500
13   925939.8 2019-10-15 11:11:00             NA
14   926529.2 2019-10-17 11:04:00             NA

标签: r

解决方案


尝试,

library(dplyr)
df %>% 
 mutate(new = ifelse(propertyID == lag(propertyID), (fromLocalTime - lag(fromLocalTime))/24, 0))

推荐阅读