Sarang Manjrekar
Sarang Manjrekar

Reputation: 1991

array in R using lists

I have two lists created using list3 = list(1,2,3,4) and list4 = list(5,6,7,8).

I create an array as :

myarray <- array(list3, list4)

I get an output as a 56*5*6 array. Unable to understand, why is it happening so ?

Upvotes: 4

Views: 72

Answers (2)

DAXaholic
DAXaholic

Reputation: 35338

Maybe something like that

list3 <- list(1,2,3,4)
list4 <- list(5,6,7,8)
listall <- append(list3, list4)
array(listall, c(2, length(listall) / 2))

outputs

     [,1] [,2] [,3] [,4]
[1,] 1    3    5    7   
[2,] 2    4    6    8   

For the reason behind your output have a look into the docs

?array

Usage
array(data = NA, dim = length(data), dimnames = NULL)
...

So you passed your second list (list4 in your sample) as the dim parameter

Upvotes: 4

Sun Bee
Sun Bee

Reputation: 1820

Unlist and concatenate like so:

c(unlist(list3), unlist(list4))

Upvotes: 2

Related Questions