应用 group_by 和 summarise(sum) 但保留具有不相关冲突数据的列?

2023-11-24

我的问题非常类似于应用 group_by 并汇总数据,同时保留所有列的信息但我想保留那些被排除的列,因为它们在分组后发生冲突。

Label <- c("203c","203c","204a","204a","204a","204a","204a","204a","204a","204a")
Type <- c("wholefish","flesh","flesh","fleshdelip","formula","formuladelip",
          "formula","formuladelip","wholefish", "wholefishdelip")
Proportion <- c(1,1,0.67714,0.67714,0.32285,0.32285,0.32285, 
                0.32285, 0.67714,0.67714)
N <- (1:10)
C <- (1:10)
Code <- c("c","a","a","b","a","b","c","d","c","d")

df <- data.frame(Label,Type, Proportion, N, C, Code)
df

   Label           Type Proportion  N  C Code
1   203c      wholefish     1.0000  1  1    c
2   203c          flesh     1.0000  2  2    a
3   204a          flesh     0.6771  3  3    a
4   204a     fleshdelip     0.6771  4  4    b
5   204a        formula     0.3228  5  5    a
6   204a   formuladelip     0.3228  6  6    b
7   204a        formula     0.3228  7  7    c
8   204a   formuladelip     0.3228  8  8    d
9   204a      wholefish     0.6771  9  9    c
10  204a wholefishdelip     0.6771 10 10    d

total <- df %>% 
  #where the Label and Code are the same the Proportion, N and C 
  #should be added together respectively
  group_by(Label, Code) %>% 
  #total proportion should add up to 1 
  #my way of checking that the correct task has been completed
  summarise_if(is.numeric, sum)

# A tibble: 6 x 5
# Groups:   Label [?]
   Label   Code Proportion     N     C
  <fctr> <fctr>      <dbl> <int> <int>
1   203c      a    1.00000     2     2
2   203c      c    1.00000     1     1
3   204a      a    0.99999     8     8
4   204a      b    0.99999    10    10
5   204a      c    0.99999    16    16
6   204a      d    0.99999    18    18

直到这里我得到了我想要的。现在我想包括“类型”列,尽管它因值冲突而被排除。这是我想要获得的结果

# A tibble: 6 x 5
# Groups:   Label [?]
   Label   Code Proportion     N     C    Type
  <fctr> <fctr>      <dbl> <int> <int>  <fctr>
1   203c      a    1.00000     2     2    wholefish
2   203c      c    1.00000     1     1    flesh
3   204a      a    0.99999     8     8    flesh_formula
4   204a      b    0.99999    10    10    fleshdelip_formuladelip
5   204a      c    0.99999    16    16    wholefish_formula
6   204a      d    0.99999    18    18    wholefishdelip_formuladelip

我努力了ungroup()和一些变体mutate and unite但无济于事,任何建议将不胜感激


这里还有另外两个选项:

1) 将列嵌套在一列中,然后通过检查数据类型来自定义摘要:

df %>% 
    group_by(Label, Code) %>% nest() %>% 
    mutate(data = map(data, 
        ~ as.tibble(map(.x, ~ if(is.numeric(.x)) sum(.x) else paste(.x, collapse="_")))
          )
    ) %>% unnest()

# A tibble: 6 x 6
#   Label   Code                        Type Proportion     N     C
#  <fctr> <fctr>                       <chr>      <dbl> <int> <int>
#1   203c      c                   wholefish    1.00000     1     1
#2   203c      a                       flesh    1.00000     2     2
#3   204a      a               flesh_formula    0.99999     8     8
#4   204a      b     fleshdelip_formuladelip    0.99999    10    10
#5   204a      c           formula_wholefish    0.99999    16    16
#6   204a      d formuladelip_wholefishdelip    0.99999    18    18

2)分别汇总,然后连接结果:

numeric <- df %>% 
    group_by(Label, Code) %>% 
    summarise_if(is.numeric, sum)

character <- df %>% 
    group_by(Label, Code) %>% 
    summarise_if(~ is.character(.) || is.factor(.), ~ paste(., collapse="_"))

inner_join(numeric, character, by = c("Label", "Code"))
# A tibble: 6 x 6
# Groups:   Label [?]
#   Label   Code Proportion     N     C                        Type
#  <fctr> <fctr>      <dbl> <int> <int>                       <chr>
#1   203c      a    1.00000     2     2                       flesh
#2   203c      c    1.00000     1     1                   wholefish
#3   204a      a    0.99999     8     8               flesh_formula
#4   204a      b    0.99999    10    10     fleshdelip_formuladelip
#5   204a      c    0.99999    16    16           formula_wholefish
#6   204a      d    0.99999    18    18 formuladelip_wholefishdelip
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

应用 group_by 和 summarise(sum) 但保留具有不相关冲突数据的列? 的相关文章

随机推荐