简体   繁体   中英

How can I convert matrix to different matrices in R?

Here I have some codes generating a matrix like this:

N = 200
T = 10
mu_0 <- matrix(diag(1, T))
dim(mu_0) <- c(T,T)
mu_t_0 <- matrix(rep(t(mu_0), N), ncol = T, byrow = TRUE)

And generally the result looks like this

   V1   V2  V3  V4  V5  V6  V7  V8  V9 V10
1   1   0   0   0   0   0   0   0   0   0
2   0   1   0   0   0   0   0   0   0   0
3   0   0   1   0   0   0   0   0   0   0
4   0   0   0   1   0   0   0   0   0   0
5   0   0   0   0   1   0   0   0   0   0
6   0   0   0   0   0   1   0   0   0   0
7   0   0   0   0   0   0   1   0   0   0
8   0   0   0   0   0   0   0   1   0   0
9   0   0   0   0   0   0   0   0   1   0
10  0   0   0   0   0   0   0   0   0   1
11  1   0   0   0   0   0   0   0   0   0
12  0   1   0   0   0   0   0   0   0   0
13  0   0   1   0   0   0   0   0   0   0
14  0   0   0   1   0   0   0   0   0   0
15  0   0   0   0   1   0   0   0   0   0
16  0   0   0   0   0   1   0   0   0   0
17  0   0   0   0   0   0   1   0   0   0
18  0   0   0   0   0   0   0   1   0   0
19  0   0   0   0   0   0   0   0   1   0
20  0   0   0   0   0   0   0   0   0   1
...

Now for later calculation I want to split this large matrix into different small matrices like this:

Matrix One:

1  
0 
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
...

Matrix Two:

0
1
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
...

I tried the split function but I cannot get what I want. Are there any solutions?

You can use asplit to split an array or matrix by its margin .

x <- asplit(mu_t_0, 2)
str(x)
#List of 10
# $ : num [1:2000(1d)] 1 0 0 0 0 0 0 0 0 0 ...
# $ : num [1:2000(1d)] 0 1 0 0 0 0 0 0 0 0 ...
# $ : num [1:2000(1d)] 0 0 1 0 0 0 0 0 0 0 ...
# $ : num [1:2000(1d)] 0 0 0 1 0 0 0 0 0 0 ...
# $ : num [1:2000(1d)] 0 0 0 0 1 0 0 0 0 0 ...
# $ : num [1:2000(1d)] 0 0 0 0 0 1 0 0 0 0 ...
# $ : num [1:2000(1d)] 0 0 0 0 0 0 1 0 0 0 ...
# $ : num [1:2000(1d)] 0 0 0 0 0 0 0 1 0 0 ...
# $ : num [1:2000(1d)] 0 0 0 0 0 0 0 0 1 0 ...
# $ : num [1:2000(1d)] 0 0 0 0 0 0 0 0 0 1 ...
# - attr(*, "dim")= int 10

This one puts all the columns into a list.

res <- lapply(1:ncol(mu_t_0), function(i) mu_t_0[, i, drop=F])

head(res[[1]])
#      [,1]
# [1,]    1
# [2,]    0
# [3,]    0
# [4,]    0
# [5,]    0
# [6,]    0

head(res[[2]])
#      [,1]
# [1,]    0
# [2,]    1
# [3,]    0
# [4,]    0
# [5,]    0
# [6,]    0

To extract the single one column matrices use list2env ; the objects in the list need names beforehand.

names(res) <- paste0("m.", 1:length(res))
list2env(res, env=.GlobalEnv)
ls()
# [1] "m.1"    "m.10"   "m.2"    "m.3"    "m.4"    "m.5"    "m.6"    "m.7"    "m.8"    "m.9"    "mu_0"   "mu_t_0"
# [13] "N"      "res"    "T" 

If you want individual vectors named in the global environment (ie object V1, V2, and etc.):

invisible(mapply(assign, names(as.data.frame(mu_t_0)),  as.data.frame(mu_t_0), MoreArgs=list(envir = globalenv())))

Though I'm really interested in why the OP doesn't want to just work with a single matrix...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM