【问题标题】:R data reshape using dplyr使用 dplyr 重塑 R 数据
【发布时间】:2019-10-24 10:59:50
【问题描述】:

我有这些数据:

library(dplyr)

df1 <- tibble(
  type = c("Animals", "Animals", "People", "People"),
  type_group = c("Dogs", "Cats", "John", "Jane"),
  analysis1 = c(32.7, 67.5, 34.6, 56.5),
  analysis2 = c(23.7, 89.4, 45.8, 98.6),
  analysis3 = c(45.7, 45.7, 23.6, 23.6),
  analysis4 = c(14.4, 45.4, 98.0, 12.2))

我想在数据中添加一些新行,使其看起来像这样:

df2 <- tibble(
  type = c("Animals", "Animals", "Animals diff", "People", "People", "People diff"),
  type_group = c("Dogs", "Cats", "Dogs and cats" ,"John", "Jane", "John and Jane"),
  analysis1 = c(32.7, 67.5, 34.8, 34.6, 56.5, 21.9),
  analysis2 = c(23.7, 89.4, 65.7, 45.8, 98.6, 52.8),
  analysis3 = c(45.7, 45.7, 0.0,  23.6, 23.6, 0.0),
  analysis4 = c(14.4, 45.4, 31.0, 98.0, 12.2, 85.8))

新行的标题为“Animals diff”,即猫的数字减去狗的数字。同样,有一个新行称为“People diff”,即 Jane 数字减去 john 数字。

我知道执行此操作的简单方法是使用 dplyr 并将新行添加为变量并使数据更宽而不是更长。但是,该格式不适用于我想要对数据执行的操作。它特别需要采用 df2 中显示的较长格式。

我想我能做的是在dplyr 中使用mutate 创建变量以使数据更宽,然后使用reshape 使数据变长,但在玩了之后我想不出该怎么做那。关于如何进入 df2 的任何想法?

谢谢

【问题讨论】:

    标签: r dplyr reshape


    【解决方案1】:

    rbind 不能解决问题?

    > rbind(df1,c("People",'Me',1,2,3,4))
    # A tibble: 5 x 6
      type    type_group analysis1 analysis2 analysis3 analysis4
      <chr>   <chr>      <chr>     <chr>     <chr>     <chr>    
    1 Animals Dogs       32.7      23.7      45.7      14.4     
    2 Animals Cats       67.5      89.4      45.7      45.4     
    3 People  John       34.6      45.8      23.6      98       
    4 People  Jane       56.5      98.6      23.6      12.2     
    5 People  Me         1         2         3         4        
    

    为每个新行执行此操作,或使用您需要添加的新行创建 tibble,并以相同的方式绑定两个 tibble。

    > rbind(df1,df2)
    # A tibble: 10 x 6
       type         type_group    analysis1 analysis2 analysis3 analysis4
       <chr>        <chr>             <dbl>     <dbl>     <dbl>     <dbl>
     1 Animals      Dogs               32.7      23.7      45.7      14.4
     2 Animals      Cats               67.5      89.4      45.7      45.4
     3 People       John               34.6      45.8      23.6      98  
     4 People       Jane               56.5      98.6      23.6      12.2
     5 Animals      Dogs               32.7      23.7      45.7      14.4
     6 Animals      Cats               67.5      89.4      45.7      45.4
     7 Animals diff Dogs and cats      34.8      65.7       0        31  
     8 People       John               34.6      45.8      23.6      98  
     9 People       Jane               56.5      98.6      23.6      12.2
    10 People diff  John and Jane      21.9      52.8       0        85.8
    
    

    【讨论】:

    • 嗨,是的,这实际上很有意义。您能否分享您的第二块代码(关于您如何开发 df2?)。我了解方法,但不确定如何进行计算。谢谢
    • 其实我觉得我比较明白这一点。你破解了行中的数字吗?我需要代码来自动计算差异,而不是我手动添加它们(如果你这样做了)
    • 你好,df1和df2和你的一样。只是为了表明您可以在不更改列类型的情况下对它们进行 rbind。
    【解决方案2】:

    我想我设法使用rbind 的建议回答了这个问题。

    rbind(df1,c("People diff","John and Jane", 
            df1$analysis1[df1$type_group == 'John'] - df1$analysis1[df1$type_group == 'Jane'],
            df1$analysis2[df1$type_group == 'John'] - df1$analysis2[df1$type_group == 'Jane'],
            df1$analysis3[df1$type_group == 'John'] - df1$analysis3[df1$type_group == 'Jane'],
            df1$analysis4[df1$type_group == 'John'] - df1$analysis4[df1$type_group == 'Jane'])) -> jj
    
    rbind(df1,c("Animals diff","Dogs and cats", 
            df1$analysis1[df1$type_group == 'Cats'] - df1$analysis1[df1$type_group == 
    'Dogs'],
            df1$analysis2[df1$type_group == 'Cats'] - df1$analysis2[df1$type_group == 
    'Dogs'],
            df1$analysis3[df1$type_group == 'Cats'] - df1$analysis3[df1$type_group == 
    'Dogs'],
            df1$analysis4[df1$type_group == 'Cats'] - df1$analysis4[df1$type_group == 
    'Dogs'])) -> c_d
    
    rbind(jj, c_d)
    
    
    # A tibble: 10 x 6
      type         type_group    analysis1 analysis2 analysis3 analysis4
    <chr>        <chr>         <chr>     <chr>     <chr>     <chr>    
    1 Animals      Dogs          32.7      23.7      45.7      14.4     
    2 Animals      Cats          67.5      89.4      45.7      45.4     
    3 People       John          34.6      45.8      23.6      98       
    4 People       Jane          56.5      98.6      23.6      12.2     
    5 People diff  John and Jane -21.9     -52.8     0         85.8     
    6 Animals      Dogs          32.7      23.7      45.7      14.4     
    7 Animals      Cats          67.5      89.4      45.7      45.4     
    8 People       John          34.6      45.8      23.6      98       
    9 People       Jane          56.5      98.6      23.6      12.2     
    10 Animals diff Dogs and cats 34.8      65.7      0         31
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2015-02-12
      相关资源
      最近更新 更多