代码之家  ›  专栏  ›  技术社区  ›  Rtist

在R中,将新行添加到dataframe中的每个id

  •  0
  • Rtist  · 技术社区  · 7 年前

    我有以下数据框:

    df = data.frame(id = rep(101:110, each = 2),
                    variable = rep(c('a','b'), times = 2),
                    score = rnorm(1)) 
    

    对于每个id,我想将“variable”中的值“c”和“score”中的值100相加。我找到的唯一方法是使用rbind。

    df_helper = data.frame(id = unique(df$id),
                           variable = 'c',
                           score = 100)
    
    rbind(df, df_helper)  
    

    这不是很优雅,因为我需要定义另一个数据帧。有更好的主意吗?

    2 回复  |  直到 7 年前
        1
  •  1
  •   moodymudskipper    7 年前

    这个 tidyverse 方法是:

    library(tidyverse)
    df %>% 
      spread(variable,score) %>%
      mutate(c = 100) %>%
      gather(variable,score, - id)
    # id variable       score
    # 1  101        a  -0.1831428
    # 2  102        a  -0.1831428
    # 3  103        a  -0.1831428
    # 4  104        a  -0.1831428
    # 5  105        a  -0.1831428
    # 6  106        a  -0.1831428
    # 7  107        a  -0.1831428
    # 8  108        a  -0.1831428
    # 9  109        a  -0.1831428
    # 10 110        a  -0.1831428
    # 11 101        b  -0.1831428
    # 12 102        b  -0.1831428
    # 13 103        b  -0.1831428
    # 14 104        b  -0.1831428
    # 15 105        b  -0.1831428
    # 16 106        b  -0.1831428
    # 17 107        b  -0.1831428
    # 18 108        b  -0.1831428
    # 19 109        b  -0.1831428
    # 20 110        b  -0.1831428
    # 21 101        c 100.0000000
    # 22 102        c 100.0000000
    # 23 103        c 100.0000000
    # 24 104        c 100.0000000
    # 25 105        c 100.0000000
    # 26 106        c 100.0000000
    # 27 107        c 100.0000000
    # 28 108        c 100.0000000
    # 29 109        c 100.0000000
    # 30 110        c 100.0000000
    
        2
  •  0
  •   akrun    7 年前

    我们可以用 bind_rows

    library(tidyverse)
    bind_rows(df, df_helper)
    

    如果没有“df_helper”数据集,一种方法是

    df %>% 
        group_by(id) %>% 
        nest %>% 
        mutate(data = map(data, ~ 
                          .x %>% 
                            bind_rows(tibble(variable = "c", score = 100)))) %>%
        unnest
    # A tibble: 30 x 3
    #      id variable    score
    #   <int> <chr>       <dbl>
    # 1   101 a          -0.778
    # 2   101 b          -0.778
    # 3   101 c         100    
    # 4   102 a          -0.778
    # 5   102 b          -0.778
    # 6   102 c         100    
    # 7   103 a          -0.778
    # 8   103 b          -0.778
    # 9   103 c         100    
    #10   104 a          -0.778
    # ... with 20 more rows
    

    或者使用'data.table'的另一个选项。按“id”分组,分别将“c”和100连接到末尾的每个列

    library(data.table)
    setDT(df)[, .(variable = c(variable, 'c'), score = c(score, 100)), by = id]