Get all possible combinations by row in matrix - r

I have an m x n matrix that looks like this:
1 2 3
4 5 6
What is the fastest way to get all possible combinations by row? In this case, that would be c(1,4), c(1,5), c(1,6), c(2,4), c(2,5) ... c(3,5), c(3,6)
How do I solve this using a vectorized approach? In general an m x n matrix would have n^m such combinations.

You can use the expand.grid function to get all combinations of the elements in each row, building a list of rows using split as shown here and passing each element of that list to expand.grid with the do.call function:
(m <- rbind(1:3, 4:6))
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
do.call(expand.grid, split(m, rep(1:nrow(m), ncol(m))))
# 1 2
# 1 1 4
# 2 2 4
# 3 3 4
# 4 1 5
# 5 2 5
# 6 3 5
# 7 1 6
# 8 2 6
# 9 3 6
Here's an example with a 3 x 2 matrix instead of a 2 x 3 matrix:
(m <- matrix(1:6, nrow=3))
# [,1] [,2]
# [1,] 1 4
# [2,] 2 5
# [3,] 3 6
do.call(expand.grid, split(m, rep(1:nrow(m), ncol(m))))
# 1 2 3
# 1 1 2 3
# 2 4 2 3
# 3 1 5 3
# 4 4 5 3
# 5 1 2 6
# 6 4 2 6
# 7 1 5 6
# 8 4 5 6

Related

How to multiply only the odd rows of a matrix while still keeping the even rows?

If there were a matrix, example:
m <- matrix(c(1:4), nrow = 4, ncol=4)
how would I go about multiplying only the odd rows by an arbitrary scalar while still keeping the even rows in the same place and the same value?
In this example, the matrix with only odd rows being multiplied by two would become:
1 1 1 1 2 2 2 2
2 2 2 2 2 2 2 2
3 3 3 3 ----> 6 6 6 6
4 4 4 4 4 4 4 4
You can pass bool vector to matrix, and if it divisible with number of rows r will replicate it.
mat <- matrix(1:4,4,4)
mat[c(TRUE,FALSE),] <- mat[c(TRUE,FALSE),] * 2
[,1] [,2] [,3] [,4]
[1,] 2 2 2 2
[2,] 2 2 2 2
[3,] 6 6 6 6
[4,] 4 4 4 4
Here is a base R code using diag() + rep()
diag(rep(c(2,1),nrow(mat)/2))%*% mat
such that
> diag(rep(c(2,1),nrow(mat)/2))%*% mat
[,1] [,2] [,3] [,4]
[1,] 2 2 2 2
[2,] 2 2 2 2
[3,] 6 6 6 6
[4,] 4 4 4 4

What is the best way to tidy a matrix in R

Is there a best practice means of "tidying" a matrix/array? By "tidy" in this context I mean
one row per element of the matrix
one column per dimension. the elements of these columns give you the "coordinates" of the matrix element which is stored on that row
I have an example here for a 2d matrix, but ideally this would work with an array also (This example works for mm <- array(1:18, c(3,3,3)), but I thought that would be too much to paste in here)
mm <- matrix(1:9, nrow = 3)
mm
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 9
inds <- which(mm > -Inf, arr.ind = TRUE)
cbind(inds, value = mm[inds])
#> row col value
#> [1,] 1 1 1
#> [2,] 2 1 2
#> [3,] 3 1 3
#> [4,] 1 2 4
#> [5,] 2 2 5
#> [6,] 3 2 6
#> [7,] 1 3 7
#> [8,] 2 3 8
#> [9,] 3 3 9
as.data.frame.table One way to convert from wide to long is the following. See ?as.data.frame.table for more information. No packages are used.
mm <- matrix(1:9, 3)
long <- as.data.frame.table(mm)
The code gives this data.frame:
> long
Var1 Var2 Freq
1 A A 1
2 B A 2
3 C A 3
4 A B 4
5 B B 5
6 C B 6
7 A C 7
8 B C 8
9 C C 9
numbers
If you prefer row and column numbers:
long[1:2] <- lapply(long[1:2], as.numeric)
giving:
> long
Var1 Var2 Freq
1 1 1 1
2 2 1 2
3 3 1 3
4 1 2 4
5 2 2 5
6 3 2 6
7 1 3 7
8 2 3 8
9 3 3 9
names Note that above it used A, B, C, ... because there were no row or column names. They would have been used if present. That is, had there been row and column names and dimension names the output would look like this:
mm2 <- array(1:9, c(3, 3), dimnames = list(A = c("a", "b", "c"), B = c("x", "y", "z")))
as.data.frame.table(mm2, responseName = "Val")
giving:
A B Val
1 a x 1
2 b x 2
3 c x 3
4 a y 4
5 b y 5
6 c y 6
7 a z 7
8 b z 8
9 c z 9
3d
Here is a 3d example:
as.data.frame.table(array(1:8, c(2,2,2)))
giving:
Var1 Var2 Var3 Freq
1 A A A 1
2 B A A 2
3 A B A 3
4 B B A 4
5 A A B 5
6 B A B 6
7 A B B 7
8 B B B 8
2d only For 2d one can alternately use row and col:
sapply(list(row(mm), col(mm), mm), c)
or
cbind(c(row(mm)), c(col(mm)), c(mm))
Either of these give this matrix:
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 1 2
[3,] 3 1 3
[4,] 1 2 4
[5,] 2 2 5
[6,] 3 2 6
[7,] 1 3 7
[8,] 2 3 8
[9,] 3 3 9
Another method is to use arrayInd together with cbind like this.
# a 3 X 3 X 2 array
mm <- array(1:18, dim=c(3,3,2))
Similar to your code, but with the more natural arrayInd function, we have
# get array in desired format
myMat <- cbind(c(mm), arrayInd(seq_along(mm), .dim=dim(mm)))
# add column names
colnames(myMat) <- c("values", letters[24:26])
which returns
myMat
values x y z
[1,] 1 1 1 1
[2,] 2 2 1 1
[3,] 3 3 1 1
[4,] 4 1 2 1
[5,] 5 2 2 1
[6,] 6 3 2 1
[7,] 7 1 3 1
[8,] 8 2 3 1
[9,] 9 3 3 1
[10,] 10 1 1 2
[11,] 11 2 1 2
[12,] 12 3 1 2
[13,] 13 1 2 2
[14,] 14 2 2 2
[15,] 15 3 2 2
[16,] 16 1 3 2
[17,] 17 2 3 2
[18,] 18 3 3 2

Generate all permutations of a matrix in R

I have a matrix M given by the following:
M <- matrix(1:6, nrow=2, byrow=TRUE)
1 2 3
4 5 6
and I wish to generate all possible permutations for this matrix as a list. After reading Generating all distinct permutations of a list in R, I've tried using
library(combinat)
permn(M)
but this gives the me all the permutations as a single row, and not the 2 x 3 matrix I had originally.
So what I get is something like
[[1]]
[1] 1 4 2 5 3 6
[[2]]
[1] 1 4 2 5 6 3
[[3]]
[1] 1 4 2 6 5 3
...
[[720]]
[1] 4 1 2 5 3 6
But what I want is to keep the first and second rows distinct from each other so it would be a list that looks more like the following:
[[1]]
1 2 3
4 5 6
[[2]]
1 3 2
4 5 6
[[3]]
2 3 1
5 4 6
...
until I get all possible combinations of M. Is there a way to do this in R?
Thank you!
How about this, using expand.grid to get all the possibilities of combinations?
M <- matrix(1:6, nrow=2, byrow=TRUE)
pcM <- permn(ncol(M))
expP <- expand.grid(1:length(pcM), 1:length(pcM))
Map(
function(a,b) rbind( M[1, pcM[[a]]], M[2, pcM[[a]]] ),
expP[,1],
expP[,2]
)
#[[1]]
# [,1] [,2] [,3]
#[1,] 1 2 3
#[2,] 4 5 6
#
#...
#
#[[36]]
# [,1] [,2] [,3]
#[1,] 2 1 3
#[2,] 5 4 6

Creating matrix from random block rows of another matrix without loop (in R)?

I am trying to create a matrix by drawing random block rows from another matrix. I have managed to do so with a loop.
set.seed(1)
a_matrix <- matrix(1:10,10,5) # the matrix with original sample
b_matrix <- matrix(NA,10, 5) # a matrix to store the bootstrap sample
S2<- seq(from =1 , to = 10, by =2) #[1] 1 3 5 7 9
m <- 2 # block size of m
for (r in S2){ start_point<-sample(1:(nrow(a_matrix)-1), 1, replace=T)
#randomly choose a number 1 to length of a_matrix -1
b_block <- a_matrix[start_point:(start_point+(m-1)), 1:ncol(a_matrix)]
# randomly select blocks from matrix a
b_matrix[r,]<-as.matrix((b_block)[1,])
b_matrix[(r+1),]<-as.matrix((b_block)[2,]) # put the blocks into matrix b
}
b_matrix
#we now have a b_matrix that is made of random blocks (size m=2)
#of the original a_matrix
The loop method works but it is clearly not very efficient and it is not possible to extend it to other block size (for e.g. having a blocksize of 3) .What is a cleaner and expandable approach ? Thanks in advance
Here I tried to clean it up a bit and generalize the use of m:
random_block_sample <- function(a_matrix, m = 2L) {
N <- nrow(a_matrix)
stopifnot(m <= N)
n <- ceiling(N / m)
s <- sample(N - m + 1L, n, TRUE) # start_point
i <- unlist(lapply(s, seq, length.out = m))
b_matrix <- a_matrix[i, , drop = FALSE]
head(b_matrix, N)
}
set.seed(1L)
random_block_sample(a_matrix, m = 2L)
# [,1] [,2] [,3] [,4] [,5]
# [1,] 3 3 3 3 3
# [2,] 4 4 4 4 4
# [3,] 4 4 4 4 4
# [4,] 5 5 5 5 5
# [5,] 6 6 6 6 6
# [6,] 7 7 7 7 7
# [7,] 9 9 9 9 9
# [8,] 10 10 10 10 10
# [9,] 2 2 2 2 2
# [10,] 3 3 3 3 3
set.seed(1L)
random_block_sample(a_matrix, m = 5L)
# [,1] [,2] [,3] [,4] [,5]
# [1,] 2 2 2 2 2
# [2,] 3 3 3 3 3
# [3,] 4 4 4 4 4
# [4,] 5 5 5 5 5
# [5,] 6 6 6 6 6
# [6,] 3 3 3 3 3
# [7,] 4 4 4 4 4
# [8,] 5 5 5 5 5
# [9,] 6 6 6 6 6
# [10,] 7 7 7 7 7

remove a value and its corresponding value in a table

In a file (1000 columns, 2000 rows), for each column there is another column next to it. something like:
[,1] [,2] [,3] [,4] [,5] [,6]
3 3 4 4 4 6
6 5 2 2 5 1
9 1 3 5 4 1
2 5 6 4 8 5
6 1 5 2 3 1
I want to remove those values which their corresponding value is 1
the result:
[,1] [,3] [,5]
3 4 4
6 2 8
2 3
6
5
To echo what #shellter said, it's both helpful and polite to include what you've tried in the question.
Here's a compact way to accomplish this using split and mapply.
d <- read.table(text='3 3 4 4 4 6
6 5 2 2 5 1
9 1 3 5 4 1
2 5 6 4 8 5
6 1 5 2 3 1', header=FALSE)
cols <- split(as.list(d), rep(1:2, length.out=length(d)))
mapply(function(col1, col2) col1[col2 != 1],
cols[[1]], cols[[2]], SIMPLIFY=FALSE)
# $V1
# [1] 3 6 2
#
# $V3
# [1] 4 2 3 6 5
#
# $V5
# [1] 4 8

Resources