How to initialize an m*n matrix in R with specific row and column names - r

I am trying to find a way to initialize a m*n matrix in R.
Let's say I have a seq of variable names c(a, b, c, d), and I would like to create a 4*10 matrix with c(a, b, c, d) being the vertical variable, and seq(1:10) to be horizontal variable, so I can check the matrix with the call matrix[a, 1].
Thanks in advance

We can create the matrix as
m1 <- matrix(nrow = 4, ncol = 10, dimnames = list(letters[1:4], NULL))
and use the row names and column index to extract elements
m1['a', 1]

Another base R option using row.names<-
`row.names<-`(matrix(nrow = 4, ncol = 10), head(letters, 4))

Related

Replacing values in the columns of a matrix according to a vector of indices?

I have a matrix of zeroes:
M <- matrix(0, nrow = 10, ncol = 5)
and a vector of indices
V <- c(1,5,3,2,3,4,1,3,2,4)
I want to replace the entries M[i,V[i]] by 1, i in 1:10. How can I do this without using brute force (for loop)? Below is the code to do so using brute force, which is not efficient in higher dimensions:
for(i in 1:10) M[i,V[i]] = 1
You can make a matrix from your vector V and use it directly, i.e.
M[matrix(c(seq_along(V), V), ncol = 2)] <- 1

Identify row in a matrix corresponding to a vector

Assume a matrix that contains all bit strings of length r and is in order.
library(gtools)
mat<-permutations(n = 2, r = 5, v = c(0,1), repeats.allowed = TRUE)
mat<-cbind(mat, round(runif(nrow(mat)), digits = 2))
and several vectors each with r elements:
r=5
vec<-t(replicate(100,sample(c(0,1),5,replace=T)))
For each vector (i.e, row in vec) I would like to identify the corresponding row in mat
Note: I would like to list the result for each row, not just the unique elements.
Is there an efficient way to do this without using a for loop?
Try
indx1 <- do.call(`paste0`,as.data.frame(mat[,-6]))
indx2 <- do.call(`paste0`, as.data.frame(vec))
sapply(indx2, function(x) mat[indx1 %in% x,6])

Creating block matrix via loop

I'm trying to create a block matrix using a loop in R, which depend on some variable I call T. The two matrices used to construct the block matrix could look like this:
A=matrix(c(1,0.3,0.3,1.5),nrow=2)
B=matrix(c(0.5,0.3,0.3,1.5),nrow=2)
So depending on what i set T to, I need different results. For T=2:
C=rbind(cbind(A,B),cbind(B,A))
For T=3:
C=rbind(cbind(A,B,B),cbind(B,A,B),cbind(B,B,A))
For T=5:
C=rbind(cbind(A,B,B,B,B),cbind(B,A,B,B,B),cbind(B,B,A,B,B),cbind(B,B,B,A,B),cbind(B,B,B,B,A))
So basically, I'm just trying to create a loop or something similar, where I can just specify my T and it will create the block matrix for me depending on T.
Thanks
You can do that:
N <- nrow(A)
C <- matrix(NA,N*T,N*T)
for (i in 1:T){
for (j in 1:T){
if (i == j)
C[(i-1)*N+1:N, (j-1)*N+1:N] <- A
else
C[(i-1)*N+1:N, (j-1)*N+1:N] <- B
}
}
From your explanation I suppose that you want single A and T-1 Bs in your final matrix.
If that is correct then here is a quick try using the permn function from the combinat library. All I am doing is generating the expression using the permutation and then evaluating it.
A = matrix(c(1,0.3,0.3,1.5),nrow=2)
B = matrix(c(0.5,0.3,0.3,1.5),nrow=2)
T = 5
x = c("A", rep("B",T-1))
perms = unique(permn(x)) #permn generates non-unique permutations
perms = lapply(perms, function(xx) {xx=paste(xx,collapse=","); xx=paste("cbind(",xx,")")})
perms = paste(perms, collapse=",")
perms = paste("C = rbind(",perms,")",collapse=",")
eval(parse(text=perms))
With the blockmatrix package this is pretty straightforward.
library(blockmatrix)
# create toy matrices (block matrix elements)
# with values which makes it easier to track them in the block matrix in the example here
A <- matrix("a", nrow = 2, ncol = 2)
B <- matrix("b", nrow = 2, ncol = 2)
# function for creating the block matrix
# n: number of repeating blocks in each dimension
# (I use n instead of T, to avoid confusion with T as in TRUE)
# m_list: the two matrices in a list
block <- function(n, m_list){
# create a 'layout matrix' of the block matrix elements
m <- matrix("B", nrow = n, ncol = n)
diag(m) <- "A"
# build block matrix
as.matrix(blockmatrix(dim = dim(m_list[[1]]), value = m, list = m_list))
}
# try with different n
block(n = 2, m_list = list(A = A, B = B))
block(n = 3, m_list = list(A = A, B = B))
block(n = 5, m_list = list(A = A, B = B))

Combining many matrices with different names in R

I have 141 matrices with the same dimensions, but with different names like:
mat_1, mat_55, mat_154, ...
I have their names in another matrix:
"mat_1" , "mat_55" , ...
And now I'm trying to combine all of them in a single matrix. Should I write the name of all of them manually in rbind(), or there is another way?
rbind(mat_1,mat_55,....)
mat_1 = matrix(1:10, ncol = 2)
mat_2 = matrix(11:20, ncol = 2)
mat_3 = matrix(21:30, ncol = 2)
names = c('mat_1','mat_2','mat_3')
x = lapply(lapply(names, as.symbol), eval)
do.call("rbind", x)
You can use
do.call(rbind, mget(mat_names))
where mat_names is the name of you vector including matrix names.

How to get all rows of a dataframe NOT designated in a vector in R?

I'm looking for an elegant, R-like way to capture rows in a dataframe that don't have their indices listed in a vector:
table.combos <- matrix(data = 1:12, nrow = 10, ncol = 6, byrow=T)
table.combos
not.these<-c(2,4,5,9)
x<-table.combos[c(not.these),]
#y<- everything not in x
Just use the same index vector as in:
y <- table.combos[-not.these,]
which tells chose all the rows from table.combos but those contained in not.these vector.

Resources