 # Converting a Matrix into a List

I have the following matrix: `mymatrix`:

``````> mymatrix
[,1]
1   111.22
2   222.33
3   333.44
4   444.55
5   555.66

> class(mymatrix)
 "matrix"
``````

My Question is: How can I create a list: `mylist` from `mymatrix`, so I get the following:

``````> mylist
[]
[,1]
1   111.22
2   222.33
3   333.44
4   444.55
5   555.66

> class(mylist)
 "list"
``````

As you can see, when you show them on the console their data look almost the same.

By the way, could you provide the code to create the matrix and the list based on that matrix?

I tried different things to create the matrix that way, including the following code:

``````# unwanted result
> mymatrix = data.matrix(c(111.22, 222.33, 333.44, 444.55, 555.66))
[,1]
[1,] 111.22
[2,] 222.33
[3,] 333.44
[4,] 444.55
[5,] 555.66
``````

where you can see that the row names are different from the ones on the first matrix.

What I need to do, is just to compare, given a `Neural Network`, the `nn\$net.result` (after training) with the: `output\$net.result` (after prediction). I know we normally don't need to do that, but for certain reasons I need to do it. My problem here is that their data type are different as you can see below:

``````> nn = neuralnet(target ~ ., data = ds_training, hidden = 2, err.fct = "sse")
> class(nn\$net.result)
 "list"

# Neural Network
> output = neuralnet::compute(nn, ds_training)
> class(output\$net.result)
 "matrix"
``````

Thanks!

Is this what you had in mind:

``````x = matrix(1:5)

x.list = list(x = x)
``````
``````> x
[,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5

> x.list
\$x
[,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
``````
``````> class(x)
 "matrix"

> class(x.list)
 "list"

> class(x.list\$x)
 "matrix"
``````

Just going to a list from the matrix creates a new list element for each row. But when you go via a data.frame, it creates a vector for the column, which then translates to what you want.

``````mymatrix <- matrix(nrow = 5, ncol = 1, data = c(111.11, 222.22, 333.33,444.44, 555.55))

> mymatrix
[,1]
[1,] 111.11
[2,] 222.22
[3,] 333.33
[4,] 444.44
[5,] 555.55
``````
``````> mylist <- list(as.data.frame(mymatrix))
> mylist
[]
V1
1 111.11
2 222.22
3 333.33
4 444.44
5 555.55
``````

@joels, unfortunatelly that doesn' work. Your row names have a different format. I updated my initial post.

Thanks!

@mdzorn I tried your approach with my original data:

``````> str(nn\$net.result)
List of 1
\$ : num [1:791, 1] 4.17 4.25 4.22 3.73 4.28 ...
..- attr(*, "dimnames")=List of 2
.. ..\$ : chr [1:791] "1" "2" "3" "4" ...
.. ..\$ : NULL

> str(mylist)
List of 1
\$ :'data.frame':	791 obs. of  1 variable:
..\$ V1: num [1:791] 4.17 4.25 4.22 3.73 4.28 ...
``````

But it seems not to be working.

Then, if I try to compare, I get the following:

``````> mylist == nn\$net.result
Error in mylist == nn\$net.result :
comparison of these types is not implemented
``````

Thanks!

Check out this thread on stackoverflow:

where the fastest method is:

``````lapply(seq_len(ncol(x)), function(i) x[, I])

mylist <-  lapply(seq_len(ncol(output\$net.result)), function(i) output\$net.result[, i])
``````

I'm not sure why the rownames would matter. Isn't it the actual output values that you want to compare? I think it would be helpful if you could provide a reproducible example and additional information about what exactly you're trying to compare and what would constitute a successful comparison. It's relatively straightforward to convert data between matrix, data frame, and list structures.

1 Like

If your goal is to compare two vectors, then it seems to me that you want to extract the vector from each data structure, not convert one data structure to the other? It would really help to see the result of `str(output\$net.result)`, but assuming that the plain matrix has a structure similar to the one that's wrapped in a list:

``````x.list <- list(
matrix(1:5, dimnames = list(as.character(1:5)))
)

x.mat <- matrix(c(1:4, 6L), dimnames = list(as.character(1:5)))

x.list[][, 1] == x.mat[, 1]
#>     1     2     3     4     5
#>  TRUE  TRUE  TRUE  TRUE FALSE

str(x.list[][, 1])
#>  Named int [1:5] 1 2 3 4 5
#>  - attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...

str(x.mat[, 1])
#>  Named int [1:5] 1 2 3 4 6
#>  - attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...
``````

Created on 2019-09-05 by the reprex package (v0.3.0)

1 Like

thank you guys, that helped!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.