0

I have a serie of vectors with different lengths within the R environment, for example:

> s_aleat_0
[1] "12M08393" "12M08427" "12M08412" "12M08441" "12M08436" "12M08444" "12M08443" "12M08387"
> s_aleat_1
[1] "12M08405" "12M08389" "12M08439" "12M08406" "12M08397" "12M08402"
> s_aleat_2
[1] "12M08422" "12M08441" "12M08410" "12M08391" "12M08396" "12M08420" "12M08409"

And I want call them by pattern into another funtion (rbind his character strings to join the initial vectors in a bigger one) like that:

samples<-ls(pattern = "s_aleat_")
unique(c(rbind(samples)))
     [1] "12M08393" "12M08405" "12M08422" "12M08427" "12M08389" "12M08441" "12M08412" "12M08439" "12M08410"
    [10] "12M08406" "12M08391" "12M08436" "12M08397" "12M08396" "12M08444" "12M08402" "12M08420" "12M08443"
    [19] "12M08409" "12M08387"

But when I put 'samples' inside rbind it doesn't recognize the vectors, if not the text obtained with ls function as I show below:

c(rbind(samples))
[1] "s_aleat_0" "s_aleat_1" "s_aleat_2" "s_aleat_4" "s_aleat_5"

How I could fixed it?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Alejandro
  • 37
  • 4
  • `ls` just lists vector names. You need to pass these names to `mget` to get the actual vectors. You then need to use `do.call` to pass the list of vectors as arguments to a function. You cannot `rbind` vectors of different length. – Roland Jan 15 '21 at 09:42
  • Thanks for your answer @Roland . I explained wrong, I will use rbind to join the different vectors in a bigger one, but your answer helped me, mget worked to get what I was looking for – Alejandro Jan 15 '21 at 10:05
  • As a matter of design it would be better to store the vectors in a list or environment in the first place. Then you could lapply, sapply or Map over them in the case of a list or eapply over them in the case of an environment. – G. Grothendieck Jan 15 '21 at 12:14

2 Answers2

1

You can collect all the vectors in a list using mget and append NA's for vector with shorter length to create a matrix where each vector is a column.

s_aleat_0 <- c("12M08393","12M08427","12M08412","12M08441","12M08436","12M08444","12M08443","12M08387")
s_aleat_1 <- c("12M08405", "12M08389", "12M08439", "12M08406", "12M08397" ,"12M08402")
s_aleat_2 <- c( "12M08422", "12M08441", "12M08410" ,"12M08391", "12M08396", "12M08420", "12M08409")

tmp <- mget(ls(pattern = "s_aleat_"))
result <- sapply(tmp, `[`, 1:max(lengths(tmp)))

#     s_aleat_0  s_aleat_1  s_aleat_2 
#[1,] "12M08393" "12M08405" "12M08422"
#[2,] "12M08427" "12M08389" "12M08441"
#[3,] "12M08412" "12M08439" "12M08410"
#[4,] "12M08441" "12M08406" "12M08391"
#[5,] "12M08436" "12M08397" "12M08396"
#[6,] "12M08444" "12M08402" "12M08420"
#[7,] "12M08443" NA         "12M08409"
#[8,] "12M08387" NA         NA        
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

We can use stri_list2matrix from stringi

library(stringi)
stri_list2matrix(tmp)
#     [,1]       [,2]       [,3]      
#[1,] "12M08393" "12M08405" "12M08422"
#[2,] "12M08427" "12M08389" "12M08441"
#[3,] "12M08412" "12M08439" "12M08410"
#[4,] "12M08441" "12M08406" "12M08391"
#[5,] "12M08436" "12M08397" "12M08396"
#[6,] "12M08444" "12M08402" "12M08420"
#[7,] "12M08443" NA         "12M08409"
#[8,] "12M08387" NA         NA        

data

s_aleat_0 <- c("12M08393","12M08427","12M08412","12M08441","12M08436","12M08444","12M08443","12M08387")
s_aleat_1 <- c("12M08405", "12M08389", "12M08439", "12M08406", "12M08397" ,"12M08402")
s_aleat_2 <- c( "12M08422", "12M08441", "12M08410" ,"12M08391", "12M08396", "12M08420", "12M08409")

tmp <- mget(ls(pattern = "s_aleat_"))
akrun
  • 874,273
  • 37
  • 540
  • 662