【问题标题】:generate new group containing one value from each of n other groups生成新组,其中包含来自其他 n 个组中的每个组的一个值
【发布时间】:2018-12-12 07:43:32
【问题描述】:

鉴于以下示例数据:

test_data <- tibble(
  FAMILY_MEMBER_TYPE = c(rep("Father", times = 2), rep("Mother", times = 2), 
                         rep("Daugther", times = 3), rep("Son", times = 3)),
  NAME = c("Fred", "Frank", "Mary", "Megan", "Diane", "Denise", "Daisy", 
           "Sam", "Scott", "Steve")) 

如果一个家庭中只能有一个 FAMILY_MEMBER_TYPE,那么如何创建一个新的分组变量 FAMILY_NUMBER,它显示了家庭的可能组合。

即所需输出的示例(有 2 个可能的系列):

output_data <- tibble(
   FAMILY_NUMBER = c(rep("FAMILY 1", 4), rep("FAMILY 2", 4)),
   NAME = c("Fred", "Mary", "Diane", "Sam", "Fred", "Megan", "Diane","Sam"),
   FAMILY_MEMBER_TYPE = c(rep(c("Father", "Mother", "Daughter", "Son"), 2)))

    > output_data
    # A tibble: 8 x 3
      FAMILY_NUMBER NAME  FAMILY_MEMBER
      <chr>         <chr> <chr>        
    1 FAMILY 1      Fred  Father       
    2 FAMILY 1      Mary  Mother       
    3 FAMILY 1      Diane Daughter     
    4 FAMILY 1      Sam   Son          
    5 FAMILY 2      Fred  Father       
    6 FAMILY 2      Megan Mother       
    7 FAMILY 2      Diane Daughter     
    8 FAMILY 2      Sam   Son 

编辑:我已将 test_data 更改为包含不相等数量的 FAMILY_MEMBER_TYPE,因为在实际情况中我需要将此解决方案应用于,组包含不相等数量的变量。

【问题讨论】:

  • 检查expand.grid:expand.grid(split(test_data$NAME, test_data$FAMILY_MEMBER_TYPE))
  • 感谢 Henrik,这似乎可行。太糟糕了,我想用我的实际数据输出的向量是 5791818.1Gb :/

标签: r dplyr grouping


【解决方案1】:

如果成员的数量相等且已知,您可以简单地为每个成员添加序号。

test_data <- tibble(
  FAMILY_MEMBER_TYPE = c(rep("Father", times = 3), rep("Mother", times = 3), 
                         rep("Daugther", times = 3), rep("Son", times = 3)),
  NAME = c("Fred", "Frank", "Felix", "Mary", "Megan", "Michelle", "Diane", 
           "Denise", "Daisy", "Sam", "Scott", "Steve")) 

test_data$family <- seq(1, 3)

arrange(test_data, family)

【讨论】:

  • 不幸的是,在实际情况下,我需要将此解决方案应用于所有组的变量数量不同,因此我需要一些更动态的东西。
  • 喜欢每个家庭成员的连续计数吗?
  • 不完全是。 Henrik 的代码给出了所需的输出。不过感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-03-20
  • 2018-12-17
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多