in addition earlier question r: how match/join 2 matrices of different dimensions (nrow/ncol)? need loop have list of more 1000 matrices. decide use code (from answers)
full_matrix[rownames(full_matrix) %in% rownames(small_matrix), colnames(full_matrix) %in% colnames(small_matrix)] <- small_matrix
which worked fine in case single matrix match. however, have problems implementing code loop list
of matrices
"small_matrix". here code:
full_matrix <- list() for(i in 1:length(small_matrix)) { full_matrix[i][rownames(full_matrix[i]) %in% rownames(small_matrix[i]), colnames(full_matrix[i]) %in% colnames(small_matrix[i])] <- small_matrix[i] }
i error: incorrect number of subscripts on matrix
, due wrong indexing in basic knowledge of loops. appreciate help. thanks
new edit according answer of @jim m.: answer works fine example small_matrix(ces) should created in loop in former question. here find code example:
library(abind) = c("a", "b", "c", "d", "e", "f") full_matrix = array(dim=c(6,6,2)) dimnames(full_matrix) <- list(levels(as.factor(a)), levels(as.factor(a)), c("mat1","mat2"))
as @jim m. advises. , here new code:
##### new part small matrix df_import <- data.frame(oc = c("a", "a", "b", "b", "c", "c", "d", "d"), tc = c("z", "z", "y", "y", "x", "x", "w", "w"), value = c(1,2,3,4,5,6,7,8), year = c(2010,2011)) import_year <- split(df_import, df_import$year) library(reshape2) small_matrix <- list() for(i in 1:length(unique(import_year))) { small_matrix[[length(small_matrix)+1]] <- dcast(oc ~ tc, data =import_year[[i]], value.var = "value") } small_matrix ##### end new part
and further for-loop of @jim. m:
for(i in seq_along(small_matrix_list)){ afill(full_matrix[, , i], local= true ) <- small_matrix[[i]] }
however, following error message:
error in `afill<-.default`(`*tmp*`, local = true, value = list(oc = 1:4, : 'x' must have names on dimensions corresponding in 'value'
any ideas? thanks
i create full "matrix" 3-dimensional array rather list, , fill in array list of smaller matrices looping through corresponding index of 3d dimension of array.
library(abind) = c("a", "b", "c", "d", "e", "f") full_matrix = array(dim=c(6,6,2)) dimnames(full_matrix) <- list(levels(as.factor(a)), levels(as.factor(a)), c("mat1","mat2")) small_matrix_1 = matrix(c(2, 4, 3, 1, 5, 7, 3, 1, 6), nrow=3, ncol=3) dimnames(small_matrix_1) <- list(c("b","c","e"), c("a","b","f")) small_matrix_2 = matrix(c(20, 40, 30, 10, 50, 70, 30, 10, 60), nrow=3, ncol=3) dimnames(small_matrix_2) <- list(c("b","c","e"), c("a","b","f")) small_matrix_list <- list(small_matrix_1, small_matrix_2) for(i in seq_along(small_matrix_list)){ afill(full_matrix[, , i], local= true ) <- small_matrix_list[[i]] }
giving full_matrix 3rd index corresponding each of small matrices.
, , mat1 b c d e f na na na na na na b 2 1 na na na 3 c 4 5 na na na 1 d na na na na na na e 3 7 na na na 6 f na na na na na na , , mat2 b c d e f na na na na na na b 20 10 na na na 30 c 40 50 na na na 10 d na na na na na na e 30 70 na na na 60 f na na na na na na
edit: instead of creating data.frames dcast
in previous question, cast data arrays (acast
) can inserted larger array.
library(reshape2) small_matrix <- vector(mode = "list", length = 2) # pre-allocate space in list (i in seq(import_year)){ small_matrix[[i]] <- acast(oc ~ tc, data = import_year[[i]], value.var = "value") }
Comments
Post a Comment