user8959427
user8959427

Reputation: 2067

extract specific rows from a list of matrices

I have a list of matrices which look like:

[[8]]
2 x 2 sparse Matrix of class "dsCMatrix"
               3333:10-K:2006 3333:10-K:2005
3333:10-K:2006      1.0000000      0.9752259
3333:10-K:2005      0.9752259      1.0000000

[[9]]
2 x 2 sparse Matrix of class "dsCMatrix"
               3370:10-K:2006 3370:10-K:2005
3370:10-K:2006       1.000000       0.941602
3370:10-K:2005       0.941602       1.000000

[[10]]
2 x 2 sparse Matrix of class "dsCMatrix"
               3673:10-K:2006 3673:10-K:2005
3673:10-K:2006      1.0000000      0.9745789
3673:10-K:2005      0.9745789      1.0000000

I want to extract 1 observation from each list whilst also keeping the rownames and colnames, that is (for list 10) obtain a data frame:

3673:10-K:2005_3673:10-K:2006   0.9745789

I would like to store them as a data frame using something like rbindlist once all the results have been extracted.

Data:

list <- list(new("dsCMatrix", i = c(0L, 0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 
2L), Dimnames = list(c("1750:10-K:2006", "1750:10-K:2005"), c("1750:10-K:2006", 
"1750:10-K:2005")), x = c(1, 0.929121725727165, 0.999999999999997
), uplo = "U", factors = list()), new("dsCMatrix", i = c(0L, 
0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
    c("1800:10-K:2006", "1800:10-K:2005"), c("1800:10-K:2006", 
    "1800:10-K:2005")), x = c(1.00000000000001, 0.96900670959669, 
1.00000000000002), uplo = "U", factors = list()), new("dsCMatrix", 
    i = c(0L, 0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
        c("1923:10-K:2006", "1923:10-K:2005"), c("1923:10-K:2006", 
        "1923:10-K:2005")), x = c(0.999999999999999, 0.858442889654398, 
    0.999999999999999), uplo = "U", factors = list()), new("dsCMatrix", 
    i = c(0L, 0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
        c("2488:10-K:2006", "2488:10-K:2005"), c("2488:10-K:2006", 
        "2488:10-K:2005")), x = c(1.00000000000001, 0.956371967288172, 
    1), uplo = "U", factors = list()), new("dsCMatrix", i = c(0L, 
0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
    c("2969:10-K:2006", "2969:10-K:2005"), c("2969:10-K:2006", 
    "2969:10-K:2005")), x = c(0.999999999999999, 0.861326963904054, 
1), uplo = "U", factors = list()), new("dsCMatrix", i = c(0L, 
0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
    c("3133:10-K:2006", "3133:10-K:2005"), c("3133:10-K:2006", 
    "3133:10-K:2005")), x = c(0.999999999999999, 0.93751593784196, 
0.999999999999996), uplo = "U", factors = list()), new("dsCMatrix", 
    i = c(0L, 0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
        c("3197:10-K:2006", "3197:10-K:2005"), c("3197:10-K:2006", 
        "3197:10-K:2005")), x = c(0.999999999999999, 0.963362873672737, 
    0.999999999999999), uplo = "U", factors = list()), new("dsCMatrix", 
    i = c(0L, 0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
        c("3333:10-K:2006", "3333:10-K:2005"), c("3333:10-K:2006", 
        "3333:10-K:2005")), x = c(1, 0.975225879729218, 0.999999999999999
    ), uplo = "U", factors = list()), new("dsCMatrix", i = c(0L, 
0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
    c("3370:10-K:2006", "3370:10-K:2005"), c("3370:10-K:2006", 
    "3370:10-K:2005")), x = c(0.999999999999999, 0.941602039119482, 
1), uplo = "U", factors = list()), new("dsCMatrix", i = c(0L, 
0L, 1L), p = c(0L, 1L, 3L), Dim = c(2L, 2L), Dimnames = list(
    c("3673:10-K:2006", "3673:10-K:2005"), c("3673:10-K:2006", 
    "3673:10-K:2005")), x = c(1, 0.974578948898938, 1), uplo = "U", 
    factors = list()))

Upvotes: 1

Views: 39

Answers (1)

akrun
akrun

Reputation: 887951

We can specify the row/column index along with drop argument (by default, it is TRUE)

lapply(list, function(x) x[2, 1, drop = FALSE])

Upvotes: 2

Related Questions