[R] Why the bdiag code doesn't work?

arun smartpink111 at yahoo.com
Mon Aug 19 19:18:53 CEST 2013


Hi,
It is better to ?dput() your example dataset.
temp<- 
list(structure(c(16.91569, 190.89517, 169.70239, 162.14503, 190.8952, 
2573.2651, 1966.6202, 1829.3142, 169.7024, 1966.6202, 2335.3354, 
1676.7749, 162.145, 1829.314, 1676.775, 2106.543), .Dim = c(4L, 
4L), .Dimnames = list(NULL, NULL)), structure(c(29.27022, 329.11422, 
285.28642, 272.5794, 329.1142, 4454.2464, 3189.73, 3072.6324, 
285.2864, 3189.73, 3818.7995, 2776.0347, 272.5794, 3072.6324, 
2776.0347, 3379.8037), .Dim = c(4L, 4L), .Dimnames = list(NULL, 
    NULL)), structure(c(455.0825, 5496.1986, 4659.3058, 5236.8703, 
5496.199, 85142.057, 62349.821, 63831.882, 4659.306, 62349.821, 
59095.705, 57275.576, 5236.87, 63831.88, 57275.58, 70368.44), .Dim = c(4L, 
4L), .Dimnames = list(NULL, NULL)))


library(Matrix)

#I am not using R studio.  So, this may or may not help you.


A=matrix(0,18,18) 
 A[4:15,4:15] <- bdiag(temp) 
#Error in A[4:15, 4:15] <- bdiag(temp) : 
 # number of items to replace is not a multiple of replacement length


A1<-as(A,"sparseMatrix")
A1[4:15,4:15]<- bdiag(temp)

A1
18 x 18 sparse Matrix of class "dgCMatrix"
                                                                      
 [1,] . . .   .          .         .         .       .          .     
 [2,] . . .   .          .         .         .       .          .     
 [3,] . . .   .          .         .         .       .          .     
 [4,] . . .  16.91569  190.8952  169.7024  162.145   .          .     
 [5,] . . . 190.89517 2573.2651 1966.6202 1829.314   .          .     
 [6,] . . . 169.70239 1966.6202 2335.3354 1676.775   .          .     
 [7,] . . . 162.14503 1829.3142 1676.7749 2106.543   .          .     
 [8,] . . .   .          .         .         .      29.27022  329.1142
 [9,] . . .   .          .         .         .     329.11422 4454.2464
[10,] . . .   .          .         .         .     285.28642 3189.7300
[11,] . . .   .          .         .         .     272.57940 3072.6324
[12,] . . .   .          .         .         .       .          .     
[13,] . . .   .          .         .         .       .          .     
[14,] . . .   .          .         .         .       .          .     
[15,] . . .   .          .         .         .       .          .     
[16,] . . .   .          .         .         .       .          .     
[17,] . . .   .          .         .         .       .          .     
[18,] . . .   .          .         .         .       .          .     
                                                                      
 [1,]    .         .         .          .         .         .    . . .
 [2,]    .         .         .          .         .         .    . . .
 [3,]    .         .         .          .         .         .    . . .
 [4,]    .         .         .          .         .         .    . . .
 [5,]    .         .         .          .         .         .    . . .
 [6,]    .         .         .          .         .         .    . . .
 [7,]    .         .         .          .         .         .    . . .
 [8,]  285.2864  272.5794    .          .         .         .    . . .
 [9,] 3189.7300 3072.6324    .          .         .         .    . . .
[10,] 3818.7995 2776.0347    .          .         .         .    . . .
[11,] 2776.0347 3379.8037    .          .         .         .    . . .
[12,]    .         .       455.0825  5496.199  4659.306  5236.87 . . .
[13,]    .         .      5496.1986 85142.057 62349.821 63831.88 . . .
[14,]    .         .      4659.3058 62349.821 59095.705 57275.58 . . .
[15,]    .         .      5236.8703 63831.882 57275.576 70368.44 . . .
[16,]    .         .         .          .         .         .    . . .
[17,]    .         .         .          .         .         .    . . .
[18,]    .         .         .          .         .         .    . . .

A.K.




Why the following R code doesn't work in R studio??? 

temp <- vector("list",3) 
temp 
[[1]] 
          [,1]      [,2]      [,3]     [,4] 
[1,]  16.91569  190.8952  169.7024  162.145 
[2,] 190.89517 2573.2651 1966.6202 1829.314 
[3,] 169.70239 1966.6202 2335.3354 1676.775 
[4,] 162.14503 1829.3142 1676.7749 2106.543 

[[2]] 
          [,1]      [,2]      [,3]      [,4] 
[1,]  29.27022  329.1142  285.2864  272.5794 
[2,] 329.11422 4454.2464 3189.7300 3072.6324 
[3,] 285.28642 3189.7300 3818.7995 2776.0347 
[4,] 272.57940 3072.6324 2776.0347 3379.8037 

[[3]] 
          [,1]      [,2]      [,3]     [,4] 
[1,]  455.0825  5496.199  4659.306  5236.87 
[2,] 5496.1986 85142.057 62349.821 63831.88 
[3,] 4659.3058 62349.821 59095.705 57275.58 
[4,] 5236.8703 63831.882 57275.576 70368.44 

A=matrix(0,18,18) 
A[4:15,4:15] <- bdiag(temp) 

_______________________________________________ 

The returned error is: 

Error:as(x, "CsparseMatrix") : 
  no method or default for coercing “list” to “CsparseMatrix” 

Why the bdiag code doesn't work?



More information about the R-help mailing list