如何计算R中的SQL Windows函数之类的日差

问题描述 投票:0回答:3

输入:

enter image description here

目标:

使用以下规则创建名为“dayDifference”的新列:对于每对“item-city”对,计算相关对的日差。

期望的输出:

enter image description here

  • 第1行和第2行[Pair Piza-Berlin]对应3,因为2月2日到2月4日之间有3天
  • 第3行[Pair Pizza-Hambourg]对应0,因为没有日差
  • 第4行和第5行[Pair Pasta-Hambourg]对应21,因为从10到20有21天
  • 第6行[Pair Pasta-Berlin]对应0,因为没有日差

信息:当然可以有超过2行的对[例如我可以让'pizza-berlin'对100行:如果是这样的话总是取最大值(日期)并减去最小(日期)比萨 - 柏林对。

约束:

需要在R [例如没有与数据库的外部连接]

源代码:

df <- structure(list(id = c(4848L, 4887L, 4899L, 4811L, 4834L, 4892L
), item = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Pasta", 
"Pizza"), class = "factor"), city = structure(c(1L, 1L, 2L, 2L, 
2L, 1L), .Label = c("Berlin", "Hamburg"), class = "factor"), 
    date = structure(c(17199, 17201, -643892, 17449, 17459, 17515
    ), class = "Date")), .Names = c("id", "item", "city", "date"
), row.names = c(NA, -6L), class = "data.frame")
r date-difference
3个回答
1
投票

Reduce是一个很棒的功能

library(dplyr)
df %>% 
  group_by(item, city) %>% 
  mutate(dayDifference=abs(Reduce(`-`, as.numeric(range(date)))))

# A tibble: 6 x 5
# Groups:   item, city [4]
     id   item    city       date dayDifference
  <int> <fctr>  <fctr>     <date>         <dbl>
1  4848  Pizza  Berlin 2017-02-02             2
2  4887  Pizza  Berlin 2017-02-04             2
3  4899  Pizza Hamburg 0207-02-01             0
4  4811  Pasta Hamburg 2017-10-10            10
5  4834  Pasta Hamburg 2017-10-20            10
6  4892  Pasta  Berlin 2017-12-15             0

2
投票

我会用data.table做到这一点:

library(data.table)
setDT(df)
df[, min_date := min(date), by = c("item", "city")]
df[, max_date := max(date), by = c("item", "city")]
df[, dayDifference := difftime(max_date, min_date, units = "days")]
df[, c("min_date", "max_date") := NULL]

它会给你想要的输出:

id  item    city       date             dayDifference
1: 4848 Pizza  Berlin 2017-02-02        2 days
2: 4887 Pizza  Berlin 2017-02-04        2 days
3: 4899 Pizza Hamburg 0207-02-01        0 days
4: 4811 Pasta Hamburg 2017-10-10       10 days
5: 4834 Pasta Hamburg 2017-10-20       10 days
6: 4892 Pasta  Berlin 2017-12-15        0 days

您也可以使用df[, dayDifference := max_date - min_date]而不是df[, dayDifference := difftime(max_date, min_date, units = "days")]


0
投票

不漂亮,但......

i<-unique(lapply(1:nrow(df),function(x) which(paste(df[,2],df[,3]) %in% paste(df[x,2],df[x,3]))))
for(j in 1:length(i)) df[i[[j]],"days"]<-abs(difftime(df[i[[j]],][1,"date"],df[i[[j]],][2,"date"]))

> df
    id  item    city       date days
1 4848 Pizza  Berlin 2017-02-02    2
2 4887 Pizza  Berlin 2017-02-04    2
3 4899 Pizza Hamburg 0207-02-01   NA
4 4811 Pasta Hamburg 2017-10-10   10
5 4834 Pasta Hamburg 2017-10-20   10
6 4892 Pasta  Berlin 2017-12-15   NA
© www.soinside.com 2019 - 2024. All rights reserved.