【问题标题】:How to count weekend days如何计算周末天数
【发布时间】:2019-08-14 07:34:43
【问题描述】:

我有一个数据框 CLIENTS: 酒店编号 国籍 到达日期 出发日期 类似的东西:

Client   Nationality   Hotel   Dateofarrive   DateofDeparture
Cl1          es         h1      21/07/2019    24/07/2019
Cl2          es         h1      23/07/2019    24/07/2019
Cl3          es         h1      06/07/2019    10/07/2019
Cl4          es         h2      05/07/2019    06/07/2019
Cl5          fr         h3      01/07/2019    02/07/2019
Cl6          pt         h1      07/07/2019    09/07/2019

我创建了一个数据框酒店,对于数据框的每家酒店,我都有所有西班牙国籍的客户以及他们在酒店睡觉的所有夜晚,法国国籍和葡萄牙人也是如此。 类似的东西:

Hotel    CliEspan  Nights  CliFrench Night CliPortug Night 
H1           3       8       0         0          1      2
H2           1       5       1         1          0      0

在客户入住酒店的所有夜晚中,我还想要一个周末的夜晚数。例如:

Hotel    CliEspan  Nights WdN CliFrench Night WdN CliPortug Night WdN 
H1           3       8      5    0         0   0       1      2    2
H2           1       5      3    1         1   0       0      0    0

就目前而言,我需要包括周末。

clients[, nights := as.numeric(CL_DATASORTIDA - CL_DATAENTRADA)]

clients$CL_NACIONALITAT<-as.factor(as.character(clients$CL_NACIONALITAT))

clients$CL_NACIONALITAT<-substring(clients$CL_NACIONALITAT, 1,2)
clients$AT_REGISTRECOMERC<-substring(clients$AT_REGISTRECOMERC, 1,6)

# dcast to wide format
suma <- function(x)base::sum(x, na.rm=TRUE)

hotel <- dcast(clients, AT_REGISTRECOMERC ~ CL_NACIONALITAT,  value.var = 'nights', fun.aggregate = list(suma, length))

names(hotel) <- gsub('nights_length', 'clients', names(new))

hotel<- data.frame(new)

inds <- which(colSums(hotel[, 90:177], na.rm=TRUE) < 20)

hotel$nights_other<-rowSums(hotel[, as.numeric(inds) + 1], na.rm=TRUE)

hotel$visitants_other<-rowSums(hotel[, as.numeric(inds) + 89], na.rm=TRUE)

hotel<-hotel[-c(inds+1, inds+89)]

【问题讨论】:

标签: r dataframe


【解决方案1】:

这是一种方法。

首先创建一个函数来计算日期序列中周末晚上的数量。

library(dplyr)

n_weekend_nights <- function(arrival, departure) {

    if (arrival == departure) {
      return(0)
      # arrival %>% format("%u") %>% as.numeric() %>% {. %in% 5:7}
    } else {
      seq.Date(arrival, departure-1, "day") %>%
        format("%u") %>%
        as.numeric() %>%
        {. %in% 5:7} %>%
        sum()
    }
}

然后在您的抵达和离开日期mapply它。

clients$WdN <- mapply(n_weekend_nights, clients$Dateofarrive, clients$DateofDeparture)

clients

# Client Nationality Hotel Dateofarrive DateofDeparture   WdN
# <chr>  <chr>       <chr> <date>       <date>          <int>
# Cl1    es          h1    2019-07-21   2019-07-24          1
# Cl2    es          h1    2019-07-23   2019-07-24          0
# Cl3    es          h1    2019-07-06   2019-07-10          2
# Cl4    es          h2    2019-07-05   2019-07-06          1
# Cl5    fr          h3    2019-07-01   2019-07-02          0
# Cl6    pt          h1    2019-07-07   2019-07-09          1

【讨论】:

  • 给我错误:seq.int(0, to0 - from, by) 中的错误:'by' 参数中的错误登录
  • 如果在一行或多行上,出发日期不晚于到达日期,就会发生这种情况。所有客户的晚数 >= 1 吗?
  • 它可能是 nights = 0,因为有些客户可能在酒店只待几个小时。
  • 我添加了一个处理这些客人的 if 语句。由于他们没有在酒店过夜,因此无论何时登记入住,都返回 0 是最有意义的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
相关资源
最近更新 更多