【问题标题】:Rearrange longitudinal data set to life table将纵向数据集重新排列为生命表
【发布时间】:2021-04-21 10:31:22
【问题描述】:

我有一张关于一些人的居住地和职业的表格。我想知道从事某些职业的人是否比其他人更有可能搬迁。纵向数据如下所示:

library(tidyverse)    
id <- c(rep(1, 6), rep(2, 6), rep(3, 6))
year <- c(rep(1990:1995, 3))
occupation <- c(rep("Barrister", 6), rep("Telephone salesman", 3), rep("Baker", 3), rep("Janitor", 2), rep("Builder", 4))
residence <- c(rep("London", 2), rep("Manchester", 2), rep("Glasgow", 2), rep("London", 6), rep("Liverpool", 4), rep ("Luton", 2))

df <- tibble(id, year, occupation, residence)

我想重新排列表格,使其采用生命表格式。此外,我想创建两个新变量:一个虚拟变量,用于表示个人是否在 x 年后搬迁(= 事件发生)或个人是否在 x 年后没有搬迁(= 事件被右删失),以及如果个人改变职业,则一个变量包含有关先前所从事职业的信息。我希望表格看起来像这样:

id2 <- c(rep(1, 3), rep(2, 2), rep(3, 3))         
years <- c(2, 2, 2, 3, 3, 2, 2, 2)
occupation2 <- c(rep("Barrister", 3), rep("Telephone salesman", 1), rep("Baker", 1), rep("Janitor", 1), rep("Builder", 2))
residence2 <- c(rep("London", 1), rep("Manchester", 1), rep("Glasgow", 1), rep("London", 2), rep("Liverpool", 2), rep ("Luton", 1))
relocated <- c(1,1,0,0,0,0,1,0)
experience <- c(rep(NA, 3), rep(NA, 1), rep("Telephone salesman", 1), rep(NA, 1), rep("Janitor", 2))

life.table <- tibble(id2, years, occupation2, residence2, relocated, experience)

我完全不确定如何实现这一点,任何建议都将不胜感激!

【问题讨论】:

  • 预期输出是否正确。 relocated列的值不清楚
  • 如果这个人在下一次观察中住在一个新的位置,那么这个想法是让 relocated = 1。有没有发现任何错误?

标签: r tidyverse data-wrangling


【解决方案1】:

可能,这有帮助

library(dplyr)
n <- 2
df %>%
    group_by(id) %>%
    mutate(n1 = cumsum(c(1, diff(year))), n2 = n(), n3 = n2 - n1, 
         n4 = n_distinct(residence)) %>% 
    group_by(occupation = factor(occupation, levels = unique(occupation)), 
       residence = factor(residence, levels = unique(residence)), .add = TRUE) %>%
    summarise(years = n(), relocated = +(any(n3 > n) & first(n4) > 1)) %>%
    group_by(id) %>% 
    mutate(experience = if(n_distinct(occupation) > 1)
     c(NA_character_, rep(as.character(first(occupation)), n() - 1))
     else NA_character_)

【讨论】:

  • 您在运行代码时是否收到“错误:summarise() 输入 relocated 存在问题。x 比较 (6) 仅适用于原子和列表类型”错误?
  • @johnny 我没有收到任何错误。它是基于相同的例子还是不同的例子。你的packageVersion('dplyr')是什么@
  • 与我发布的示例相同。我有版本“1.0.2”。
  • 我重新创建了对象和数据框,就像我在顶部代码块中所做的那样。然后我运行了你的代码,但随后我收到了错误消息。
  • @johnny 抱歉,我忘记将n &lt;- 2 添加为阈值。你现在可以试试吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
相关资源
最近更新 更多