Search code examples
rdatabasedataframerow

Split a dataframe by repeated rows in R?


I am looking for a way to transfor a data.frame with thousands of rows like this

  code             date        value  uname   tcode
   <chr>            <date>      <dbl> <ord>   <int>
 1 CODE1           1968-02-01   14.1   ""       NA
 2 CODE1           1968-03-01   9.50   ""       NA
 3 CODE1           1968-04-01   22.1   ""       NA
 4 CODE2           1968-02-01   15.1   ""       NA
 5 CODE2           1968-03-01   13.50  ""       NA
 6 CODE2           1968-04-01   23.1   ""       NA
 7 CODE3           1968-02-01   16.1   ""       NA
 8 CODE3           1968-03-01   15.50  ""       NA
 9 CODE3           1968-04-01   13.1   ""       NA

Into something like:

    date        CODE1  CODE2   CODE3
   <date>       <dbl>   <dbl>    <dbl> 
 1 1968-02-01   14.1    15.1     16.1
 2 1968-03-01   9.50    13.50    15.50
 3 1968-04-01   22.1    23.1     13.1
 

This seems straightforward but I am having difficulty realizing this task. Thanks!


Solution

  • With tidyverse you can use pivot_wider

    library(dplyr)
    library(tidyr)
    
    df %>% select(-c(uname,tcode)) %>% pivot_wider(names_from="code")
    # A tibble: 3 x 4
      date       CODE1 CODE2 CODE3
      <chr>      <dbl> <dbl> <dbl>
    1 1968-02-01  14.1  15.1  16.1
    2 1968-03-01   9.5  13.5  15.5
    3 1968-04-01  22.1  23.1  13.1
    

    Data

    df <- structure(list(code = c("CODE1", "CODE1", "CODE1", "CODE2", "CODE2", 
    "CODE2", "CODE3", "CODE3", "CODE3"), date = c("1968-02-01", "1968-03-01", 
    "1968-04-01", "1968-02-01", "1968-03-01", "1968-04-01", "1968-02-01", 
    "1968-03-01", "1968-04-01"), value = c(14.1, 9.5, 22.1, 15.1, 
    13.5, 23.1, 16.1, 15.5, 13.1), uname = c(NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), tcode = c(NA, NA, NA, NA, NA, NA, NA, NA, NA
    )), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
    ))