微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R中lubridate中大型数据集中的日期的计算有效方式

如何解决R中lubridate中大型数据集中的日期的计算有效方式

我有看起来像这样的数据,但是有2000万行。

library(tidyr)
library(dplyr)
library(stringr)
library(magrittr)
library(lubridate)
library(tidyverse)


df <- data.frame(
  DATE_OF_BIRTH = c("1933-03-31","1947-06-25","1901-09-02","1952-01-22","1936-07-18","2020-10-22","1930-05-18","1926-05-13"),DATE_OF_DEATH = c(NA,"2019-02-04","2017-10-27",NA,"2021-01-03",NA),) 

我想做的是

A)计算出截至2019年12月31日的老年人人数;并将它们分为年龄组

B)驱逐年龄或死亡日期不明的人

这是我正在执行的代码

#Change the missing dates of death into a format recognisable as a date,which is far into the future
df %<>%
  replace_na(list(DATE_OF_DEATH = "01/01/9999"))

#Specify the start and end date of the year of interest
end_yr_date = dmy('31/12/2019')
start_yr_date = dmy('01/01/2019')

df %<>%
  #create age
  mutate(age = floor(interval(start = dmy(DATE_OF_BIRTH),end = end_yr_date) / 
                       duration(num = 1,units = "years"))) %>%
  #and age groupings
  mutate(age_group = cut(age,breaks = c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,150),labels = c("00-04",'05-09','10-14',"15-19","20-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85+"),right = FALSE))


df %<>%
  #remove people who were born after end date
  filter(!(dmy(DATE_OF_BIRTH) > end_yr_date)) %>%
  #remove people who died before start date
  filter(!(dmy(DATE_OF_DEATH) < start_yr_date)) %>%
  #Remove people with a negative age
  filter(age >= 0) %>%
  #Remove people older than 115
  filter(age < 116)

在此示例数据集上运行良好,但仅在2000万行数据上保持运行。我想知道是否存在处理日期的方法,这些方法在计算上更有效,甚至更快?

我还想知道我是否有可能无法解析的日期格式(我删除了NA日期,但也许还有其他数据输入错误,但格式不正确),这就是为什么代码会一直保留的原因运行。有谁知道一种有效的方法来确定任何无法解析的日期格式(不是NA)?

感谢您的帮助。

解决方法

您可以将列更改为date类一次,并将所有filter表达式都包含在内。

library(dplyr)
library(lubridate)

df %>%
  mutate(across(c(DATE_OF_BIRTH,DATE_OF_DEATH),ymd),age = floor(interval(start = DATE_OF_BIRTH,end = end_yr_date) / 
                       duration(num = 1,units = "years")),age_group = cut(age,breaks = c(seq(0,85,5),150),labels = c("00-04",'05-09','10-14',"15-19","20-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85+"),right = FALSE)) %>%
  filter(DATE_OF_BIRTH < end_yr_date,DATE_OF_DEATH > start_yr_date,between(age,116)) -> result

如果仍然很慢,则可以切换到data.table

library(data.table)

setDT(df)
df[,c('DATE_OF_BIRTH','DATE_OF_DEATH') := lapply(.SD,.SDcols = c('DATE_OF_BIRTH','DATE_OF_DEATH')] %>%
  .[,age := floor(interval(start = DATE_OF_BIRTH,end = end_yr_date) / 
                duration(num = 1,units = "years"))] %>%
  .[,age_group := cut(age,right = FALSE)] %>%
  .[DATE_OF_BIRTH < end_yr_date & DATE_OF_DEATH > start_yr_date & between(age,116)] -> result

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。