0

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
joeblow
  • 13
  • 6

3 Answers3

1

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.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
0

We can do this with a nested sapply to loop over the list

 m1 <- sapply(list(A, B, C), function(x) sapply(x, paste, collapse=""))
 setNames(as.data.frame(m1), LETTERS[1:3])
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Here is one base R method.

data.frame(do.call(mapply,
                   list(function(x) sapply(x, paste, collapse=""), list(A=A, B=B, C=C))))

This returns

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

Here, do.call applies Map. The list in the second line are the arguments for Map which applies sapply over the a named list of the lists. sapply pastes the vectors of each list item and returns a vector.

Notes:

  1. this is an attempt at replicating methods alexis-laz showed me earlier here.
  2. I originally used Map, but mapply appears to be roughly 33% faster (344ms vs. 513ms) according to microbenchmark.
Community
  • 1
  • 1
lmo
  • 37,904
  • 9
  • 56
  • 69