生成包含n个其他组中每个组的一个值的新组

时间:2018-12-12 07:43:33

标签: r dplyr grouping

给出以下示例数据:

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,因为在实际情况下,我需要将此解决方案应用于该组,因此组包含不相等数量的变量。

1 个答案:

答案 0 :(得分:0)

如果成员数相等且已知,则只需在每个成员中添加顺序号即可。

docker exec -it dev-centos-1 bash