【问题标题】:Add a conditional formula column添加条件公式列
【发布时间】:2019-10-31 11:42:00
【问题描述】:

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

如果propertyID行#235364=propertyID row #235363,则从LocalTime返回(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


    【解决方案1】:

    试试,

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

    【讨论】:

    • 它适用于我的大部分数据!我将编辑帖子以分享更多我的数据以显示它不起作用的情况
    • 有些计算是正确的,有些则不是。我不知道为什么?
    • 我也没有。我得到的值与您的预期值完全相同...
    猜你喜欢
    • 2021-08-26
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多