计算两个日期之间每月的天数

时间:2018-07-13 12:51:51

标签: r date

我需要计算一张病床忙了多少天。因此,我有患者入院的开始和结束日期。

一个例子

df <- data.frame(pac=c("A","B","C"),
                 start=c("2015-10-30","2015-11-08","2015-12-08"),
                 end=c("2015-11-10","2016-01-02","2016-03-05"))

我等着找到一个数据帧。

Ano Mes Value  
15  OUT  2     
15  NOV  33    
15  DEZ  55    
16  JAN  33    
16  FEV  29    
16  MAR  5

如何在R中进行操作?

1 个答案:

答案 0 :(得分:0)

可能有一个更短的解决方案,但是我觉得这很容易理解:) 缩写的月份在我的语言环境中(荷兰语)...它会根据您的PC设置而变化..或者您可以将其设置为内联(?lubridate::month

library(tidyverse)
library(lubridate)
#create a vector of all dates within the given ranges
v <- apply( df, 1, function(x) {seq( as.Date(x[2], format = "%Y-%m-%d"), as.Date(x[3], format = "%Y-%m-%d"), by ="day" )}) %>%
  unlist() %>%
  as.Date( origin = "1970-01-01" ) 

#put the dates in a data.frame, use lubridate to extract year and month-data
df2 <- data.frame ( date = v ) %>%
  mutate( Ano = lubridate::year( date ) ) %>%
  mutate( Mes = lubridate::month( date, abbr = TRUE, label = TRUE ) ) %>%
  group_by( Ano, Mes ) %>%
  summarise( Value = n() ) %>%
  select( Ano, Mes, Value)

# > df2
# # A tibble: 6 x 3
# # Groups:   Ano [2]
#     Ano Mes   Value
#    <dbl> <ord> <int>
# 1  2015 okt       2
# 2  2015 nov      33
# 3  2015 dec      55
# 4  2016 jan      33
# 5  2016 feb      29
# 6  2016 mrt       5