# Split the list data into several single matrix data

I have a list data a, and each of a[[i]] contains different cols of data. I want to convert the list data into several matrix.

matrix1=data.frame(matrix(unlist(a[[1]]), ncol = 10, byrow=F),stringsAsFactors=FALSE);
matrix2=data.frame(matrix(unlist(a[[2]]), ncol = 10, byrow=F),stringsAsFactors=FALSE);
……
matrix50=data.frame(matrix(unlist(a[[50]]), ncol = 10, byrow=F),stringsAsFactors=FALSE)

I guess it should use the function 'lapply ', but I am not familiar with this function, so I keep getting errors. Hope to get your help. Thank you!

``````a <- list(
list(
1:2,
3:4
),
list(
5:6,
6:7
)
)

library(tidyverse)
map(a,
bind_cols)
#or
map(a,
~matrix(unlist(.),ncol=2))``````

Thank you very much for replying.

I think there should be a loop. Since the output matrix data is not all 2 columns. They are 2/3/4 cols.
And is there a way that I can get 3 matrices, not another list?

c <- list(
list(
1:2,
3:4
),
list(
5:7,
6:8
),
list(
7:10,
8:11
)
)

The method provided is flexible to the number of columns

``````a <- list(
list(
1:2,
3:4
),
list(
5:6,
6:7,
8:9
)
)

library(tidyverse)
map(a,
bind_cols)
``````

having the data.frames in a list is a convenient way to identify and refer to them, of course you could have them as independent data.frames in your environment (though I wouldnt myself)
but for that you should decide on how you will name them ?

It seems like you actually want a list of data frames.

``````
df <- do.call(rbind, c(a, stringsAsFactors=FALSE))
list_of_dfs <- split(df, as.numeric(rownames(df))
``````

If you really do want a list of matrices just add:

``````list_of_matrices <- lapply(list_of_dfs, as.matrix)
``````

Thank you very much. Poor in my programming

The function I used needs the data to be a matrix, so the list type is not suitable. And I just don't want to generate hundreds of matrices manually.
Here is the error when I run my function:
Error: Not compatible with requested type: [type=list; target=double].

I used the code you shared: d1=map(a,~matrix(unlist(.))), and the result is close to what I want to get.

But I also have to change these matrices to 10*i. Do you know how to change them to that kind of matrices?

I'm sorry, I find your wording of your requirements hard to follow, and there are some inconsistencies you need to clarify.
e.g. you say you want to work with matrices, but then in your example code for data that you would like to automate, you were explicitly getting rid of matrices and making them dataframes (but naming them to yourselv as maxtrixn

matrix50=data.frame(matrix(unlist(a[[50]]), ncol = 10, byrow=F),stringsAsFactors=FALSE)

also you are losing me when you say
d1=map(a,~matrix(unlist(.))),
this omits to set ncol's so presumably its a complete guess the matrix dimensions.
perhaps you should rather bind_cols as I suggested, but then cast the result to matrix

``````library(tidyverse)
(d1 <- map(
a,
~ as.matrix(bind_cols(.))
))

# if you want to get results with some function that requires matrices you can iterate also

d1[[3]] <- "not a matrix"
d1

(r1 <- map(
d1,
function(x) {
if (is.matrix(x))
return("happy")