Home > Software design >  Stacking columns to make more rows in DPLYR
Stacking columns to make more rows in DPLYR

Time:12-13

how can i transform this input df in dplyr

     new_s1 new_d1 new_e1     new_s2 new_d2 new_e2
1         A     ->      L          D     ->      L
2         D     ->      L          D     ->      L
3         K     ->      L          A     ->      L

to output to this expected output where i stack the first three coumns with the last 3 columns and do some column name changes

          s      d      e     
1         A     ->      L
2         D     ->      L
3         D     ->      L          
4         D     ->      L
5         K     ->      L          
6         A     ->      L

I'm assuming I should be using pivot_longer() ? but I can't figure out a solution.

CodePudding user response:

With pivot_longer you can use names_pattern to include the pattern that you want to extract from the column names. For the example shared, you can use -

tidyr::pivot_longer(df, 
             cols = everything(), 
             names_to = '.value', 
             names_pattern = 'new_(\\w)\\d ')

#   s     d     e    
#  <chr> <chr> <chr>
#1 A     ->    L    
#2 D     ->    L    
#3 D     ->    L    
#4 D     ->    L    
#5 K     ->    L    
#6 A     ->    L    

data

df <- structure(list(new_s1 = c("A", "D", "K"), new_d1 = c("->", "->", 
"->"), new_e1 = c("L", "L", "L"), new_s2 = c("D", "D", "A"), 
    new_d2 = c("->", "->", "->"), new_e2 = c("L", "L", "L")), 
class = "data.frame", row.names = c(NA, -3L))

CodePudding user response:

A dplyr only solution:

library(dplyr)

df %>% 
  select(1:3) %>% 
  bind_rows(df[4:6] %>% 
              `colnames<-` (colnames(df[1:3]))) %>% 
  rename_with(~substr(.,5,5)) %>% 
  as_tibble()
  s     d     e    
  <chr> <chr> <chr>
1 A     ->    L    
2 D     ->    L    
3 K     ->    L    
4 D     ->    L    
5 D     ->    L    
6 A     ->    L    

CodePudding user response:

Using base R with split.default

out <- data.frame(lapply(split.default(df, trimws(names(df),
          whitespace = ".*_|\\d ")), unlist))
row.names(out) <- NULL

-output

out
   d e s
1 -> L A
2 -> L D
3 -> L K
4 -> L D
5 -> L D
6 -> L A
  • Related