Combining many matrices with different names in R - 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.

Related

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

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))

Is there a way to sum together lists of data frames within a larger list?

I have a large list (z) containing 3 lists of 10 data frames. I would like to collapse this object into a list of 3 data frames where each data frame is the sum of the 10 prior data frames (think matrix addition). Here is what I am working with, keep in mind that these are fake numbers, as the real data are read in from hundreds of *.csv files
x = rep(1,100)
x = matrix(x,10,10)
x = as.data.frame(x)
y = list(x,x,x,x,x,x,x,x,x,x)
z = list(y,y,y)
The desired end product would look like this:
x1 = rep(10,100)
x1 = matrix(x,10,10)
y1 = list(x1,x1,x1)
I keep trying stuff along the lines of:
z1 = c()
for (i in 1:3){
for (j in 1:10){
z1[[i]] = sum(z[[i]][[j]])
}
}
However, this does not yield the desired output. I have also messed around with some of the the apply functions, but to no avail
Thanks in advance for your help!
We can use Reduce to sum the corresponding i, j elements in the list and collapse it to a single dataset
lapply(z, function(x) Reduce(`+`, x))
If we want to remove the last column which is not numeric
lapply(z, function(x) Reduce(`+`, lapply(x, function(y) y[-ncol(y)])))
Or it can be looped over the sequence of list
lapply(seq_along(z), function(i) Reduce(`+`, lapply(seq_along(z[[i]]),
function(j) z[[i]][[j]][-ncol(z[[i]][[j]])])))
If we want to use sum, the data.frames inside the list can be converted to an array, loop over the array with apply, specify the MARGIN and do the sum. In this option, there is also possiblity to take care of NA elements with na.rm = TRUE in sum
lapply(z, function(x) apply(array(unlist(x), c(10, 10, 10)),
1:2, sum, na.rm = TRUE))
Or make it more efficient by looping only on one dimension and use colSums
lapply(z, function(x) apply(array(unlist(x), c(10, 10, 10)), 1, colSums, na.rm = TRUE))
Or using a for loop
z1 <- replicate(length(z), matrix(0, 10, 10), simplify = FALSE)
for(i in seq_along(z)) for(j in seq_along(z[[1]])) z1[[i]] <- z1[[i]] + z[[i]][[j]]

select a specific columns in R nested list

suppose i have a list of data frames, just like this:
M1 <- data.frame(matrix(1:4, nrow = 2, ncol = 2))
M2 <- data.frame(matrix(1:9, nrow = 3, ncol = 3))
M3 <- data.frame(matrix(1:4, nrow = 2, ncol = 2))
mlist <- list(M1, M2, M3)
and now i want to select X1 columns from all of dataframes, I tried :
M.X1 <- mlist$X1
but failed with NULL:
> mlist$X1
NULL
I don't want to use for to extract each data frames' X1, is there some better way to do this ? And what if extract columns X3 ? (which means some columns may not exists in other row)
Normally you can use lapply as below:
lapply(mlist, function(x) x$X2)
The 2nd parameter you define a function right inside to pass to each member of mlist.

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])

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