Search code examples
rlistcbinddo.call

Combining lists with do.call while preserving each entry of the list on a row


Forgive me if this has been asked before...

I have multiple lists that I wish to combine as follows

A <- list(c("R", "L", "*", "T", "M", "S", "S", "S", "S", "Y"), 
  c("G", "A", "G", "P", "P", "V", "P"), 
  c("E", "G", "R", "E", "Q", "T", "K", "G", "S", "G"), 
  c("Y", "N", "N", "D", "W"), 
  c("T", "K"))

B <- list(c("G", "T", "Q", "R"), 
  c("T", "G", "L", "W", "D", "Y", "*", "L", "Q", "H", "A", "P", "H", "L", "H", "L"), 
  c("E", "E", "D", "A", "G", "G", "R", "E", "D", "S", "I", "L", "V", "N", "G", "A", "T", "P", "\"", "\"", "C", "S", "D", "Q", "S", "S", "D", "S", "P", "P", "I", "L", "E", "A", "I", "R"), 
  c("S", "M", "C", "G", "*", "I", "K", "P"), 
  c("D", "S", "P"))

C <- list(c("G", "L", "V", "L", "A", "H", "L", "R", "R", "L", "G"), 
  c("G", "S", "D", "T", "P", "V", "M", "P", "K", "L", "F"), 
  c("N", "W", "F", "E", "N", "T", "F", "D", "F", "R", "N", "K", "R", "C", "K", "*", "V"), 
  c("P", "A", "T", "R", "S", "L", "R", "R", "R", "A", "T", "A"), 
  c("I", "G", "F", "I", "P", "S", "P", "L", "R"))

What I want is this:

A           B                                       C
RL*TMSSSSY  GTQR                                    GLVLAHLRRLG
GAGPPVP     TGLWDY*LQHAPHLHL                        GSDTPVMPKLF 
EGREQTKGSG  EEDAGGREDSILVNGATP""CSDQSSDSPPILEAIR    NWFENTFDFRNKRCK*V
YNNDW       SMCG*IKP                                PATRSLRRRATA
TK          DSP                                     IGFIPSPLR

I've tried the following but this puts every character of the list on a different row equating to different number of rows for each list:

do.call(cbind, list(A,B,C))

Is there a way to get what I want?

Thx


Solution

  • If you want to use tidyverse functions, you could do

    library(tidyverse)
    lst(A,B,C) %>% map_df(map_chr, paste, collapse="")
    

    The lst() function allows use to make a list and keep the variable names. Then we map() over the the columns and, within each column, map() over the list of character vectors and collapse them.