【问题标题】:Calculating the number of years between two date variables and adding a variable of "Age" in R [duplicate]计算两个日期变量之间的年数并在R中添加“年龄”变量[重复]
【发布时间】:2020-02-25 01:48:32
【问题描述】:

我对 R 编程比较陌生,并且在尝试计算我创建的 2 个日期变量之间的年龄时遇到了问题。更喜欢使用 lubridate、tidyr、tidyverse、dplyr 包,因为我正在尝试学习这些特定的包,但我对最好的包持开放态度。数据来自拉赫曼棒球图书馆。请随意重写我糟糕的代码。

我的代码如下:

library(pacman)
p_load("tidyverse", "dplyr", "ggplot2", "lubridate", "stats", "Lahman")

#sort Batting dataset by playerID
Batting.df <- Batting[order(Batting$playerID), ]

#sort Master dataset by playerID
Master.df <- Master[order(Master$playerID), ]

#select variables to keep from Master df
Master.df <- Master.df %>% select(playerID, birthDay, birthMonth, birthYear, nameFirst, nameLast)

#merge Master.df and Batting.df
Batting.df = merge (Batting.df, Master.df, by = "playerID")

#concatenate first and last name
Batting.df <- unite(Batting.df, Name, c(nameFirst, nameLast), sep = ' ', remove = TRUE)

#drop NA values to avoid incorrect calculations of age
Batting.df <- Batting.df %>% tidyr::drop_na(c(birthDay, birthMonth, birthYear)) 

#add variable of DOB
Batting.df <- Batting.df %>% tidyr::unite(DOB, c(birthMonth, birthDay, birthYear), sep = "-") %>%
            dplyr::mutate(DOB = lubridate::parse_date_time(DOB, "mdy"))

#add variable of opening day by season
Batting.df <- Batting.df %>% dplyr::mutate(openingMonth = 4) %>% 
            dplyr::mutate(openingDay = 1) %>%
            tidyr::unite(seasonBegin, c(openingMonth, openingDay, yearID), sep = "-") %>%
            dplyr::mutate(seasonBegin = lubridate::parse_date_time(seasonBegin, "mdy"))

我的问题是如何通过查找“DOB”和“seasonBegin”之间的年数来创建和添加“Age”变量?我试过 with(), lubridate::time_length(),但不能让它们工作,我发现的例子是针对特定日期的,而不是变量。

任何帮助将不胜感激。

【问题讨论】:

  • 您能否提供一个可重现的数据集示例?见:stackoverflow.com/questions/5963269/…
  • 顺便评论一下:如果您“尝试学习 tidyverse”,那么我可以建议将 merge 替换为 inner_join 或其他十几个 _join 函数之一。
  • 你试过difftime()吗?
  • 您是否查看了 SO 搜索上的其他线程? this example 对你有用吗?
  • @dc37 要重现数据集,您可以使用install.packages("Lahman")

标签: r date lubridate calculation


【解决方案1】:

试试这个:

Batting.df %>% dplyr::mutate(openingMonth = 4) %>% 
  dplyr::mutate(openingDay = 1) %>%
  tidyr::unite(seasonBegin, c(openingMonth, openingDay, yearID), sep = "-") %>%
  dplyr::mutate(seasonBegin = lubridate::parse_date_time(seasonBegin, "mdy"),
                Age=as.numeric(difftime(seasonBegin, DOB, units="days")/365.25))

difftime 函数中没有“年”单位,因此将天数除以 365.25 并删除单位 (as.numeric)。

【讨论】:

  • 成功了,谢谢。
【解决方案2】:

这也很有效:

Batting.df <- Batting.df %>% dplyr::mutate(openingMonth = 4) %>% 
         dplyr::mutate(openingDay = 1) %>%
         tidyr::unite(seasonBegin, c(openingMonth, openingDay, yearID), sep = "-") %>%
         dplyr::mutate(seasonBegin = lubridate::parse_date_time(seasonBegin, "mdy")) %>% 
         dplyr::mutate(Age = DOB %--% seasonBegin/years(1))

【讨论】:

  • 是的,您还可以将最后两个 mutate 语句合并为一个。这是 dplyr 的一个卖点。 ;)
  • @Edward 谢谢,对于这个项目,它只是帮助我将它们分成几个步骤。不过,我很难在 dplyr 之外进行组合。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
相关资源
最近更新 更多