R: Reshaping Multiple Columns from Long to Wide

前端 未结 1 797
抹茶落季
抹茶落季 2020-11-30 14:57

Using following data:

library(tidyverse)

sample_df <- data.frame(Letter = c(\"a\", \"a\", \"a\", \"b\", \"b\"),
                        Number = c(1,2,1,         


        
相关标签:
1条回答
  • 2020-11-30 15:38

    An option would be to replace the duplicated elements by 'Letter' to NA and then in the reshaped data, remove the columns that are all NA

    library(data.table)
    out <- dcast(setDT(sample_df)[, lapply(.SD, function(x) 
         replace(x, duplicated(x), NA)), Letter], Letter ~ rowid(Letter), 
         value.var = c("Number", "Fruit"))
    nm1 <- out[, names(which(!colSums(!is.na(.SD))))]
    out[, (nm1) := NULL][]
    #   Letter Number_1 Number_2 Fruit_1 Fruit_2 Fruit_3
    #1:      a        1        2   Apple    Plum   Peach
    #2:      b        3        4    Pear   Peach    <NA>
    

    If we want to use the tidyverse approach, a similar option can be used. Note that pivot_wider is from the dev version of tidyr (tidyr_0.8.3.9000)

    library(tidyverse)
    sample_df %>% 
         group_by(Letter) %>%
         mutate_at(vars(-group_cols()), ~ replace(., duplicated(.), NA)) %>%
         mutate(rn = row_number()) %>% 
      pivot_wider(
              names_from = rn,
              values_from = c("Number", "Fruit")) %>%
      select_if(~ any(!is.na(.)))
    # A tibble: 2 x 6
    #  Letter Number_1 Number_2 Fruit_1 Fruit_2 Fruit_3
    #  <fct>     <dbl>    <dbl> <fct>   <fct>   <fct>  
    #1 a             1        2 Apple   Plum    Peach  
    #2 b             3        4 Pear    Peach   <NA>   
    
    0 讨论(0)
提交回复
热议问题