Isabel
Isabel

Reputation: 333

Convert list elements in a specific vector

I've like to convert a list elements in some kind of specific vector. In my example:

#Create a list
mylist<- list(A=c('a','b','c'),B=c(a='d',b='e',c='f'),
             C=c('g','h','i'), D=c(a='j',b='k',c='l'))
mylist
$A
[1] "a" "b" "c"

$B
  a   b   c 
"d" "e" "f" 

$C
[1] "g" "h" "i"

$D
  a   b   c 
"j" "k" "l" 

and I need to create a specific vector:

myvec<-c(test[[1]],test[[2]],test[[3]],test[[4]])

sounds like simple this vector creation, but a have 20K list elements and is not operational type in hand myvec<-c(test[[1]],...,test[[20000]]).

I try without success solutions like:

a<-rep("test[[",4)
b<-1:4
c<-rep("]],",4)
myvector<-as.vector(interaction(a,b,c,sep=""))

Any tips, please?

Upvotes: 1

Views: 45

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101064

Another base R option

myvec_out <- c(as.matrix(as.data.frame(mylist)))

yields

> myvec_out
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l"

Upvotes: 0

akrun
akrun

Reputation: 886948

We can use unlist

myvec <- unlist(mylist, use.names = FALSE)

Or with do.call c

myvec <- do.call(c, myvec)

Upvotes: 1

Related Questions