Add blank rows in between existing rows

前端 未结 3 727
小蘑菇
小蘑菇 2021-01-06 05:35

I have a dataset of 240 cases, in which I want to create a blank row after each existing row. Leaving me with 480 rows, of which half is filled and the other half is empty (

3条回答
  •  星月不相逢
    2021-01-06 06:29

    Try this:

    require(dplyr)
    
    df %>% 
      group_by(id) %>% 
      do(rbind(.,c(.$id,rep(NA,NCOL(df)-1)))) %>%
      ungroup() %>% data.frame()
    

    Output:

       id groep_MNC zkhs fbeh    pgebdat    p_age pgesl
    1   3         1    1    1 1955-12-01 42.50000     1
    2   3        NA   NA   NA              NA    NA
    3   5         1    1    1 1943-04-09 55.16667     1
    4   5        NA   NA   NA              NA    NA
    5   7         1    1    1 1958-04-10 40.25000     1
    6   7        NA   NA   NA              NA    NA
    7  10         1    1    1 1958-04-17 40.25000     1
    8  10        NA   NA   NA              NA    NA
    9  12         1    1    2 1947-11-01 50.66667     1
    10 12        NA   NA   NA              NA    NA
    11 14         1    1    2 1952-02-02 46.41667     1
    12 14        NA   NA   NA              NA    NA
    

    Sample data:

    require(data.table)
    df <- fread("id groep_MNC zkhs fbeh    pgebdat    p_age pgesl
                  3         1    1    1 1955-12-01 42.50000     1
                  5         1    1    1 1943-04-09 55.16667     1
                  7         1    1    1 1958-04-10 40.25000     1
                 10         1    1    1 1958-04-17 40.25000     1
                 12         1    1    2 1947-11-01 50.66667     1
                 14         1    1    2 1952-02-02 46.41667     1")
    

提交回复
热议问题