I am trying to convert the rows of the matrices below into indices to subset from another matrix. The first matrix would generate four indices to be used for subsetting from a matrix called data, shown at the bottom. The second matrix would generate six indices, each of length two, and so on.
library(gtools) # library for combinations()
matrix(match(combinations(n=4,r=1,v=LETTERS[1:4]),LETTERS),ncol=1)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
matrix(match(combinations(n=4,r=2,v=LETTERS[1:4]),LETTERS),ncol=2)
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 2 3
[5,] 2 4
[6,] 3 4
matrix(match(combinations(n=4,r=3,v=LETTERS[1:4]),LETTERS),ncol=3)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 3 4
[4,] 2 3 4
matrix(match(combinations(n=4,r=4,v=LETTERS[1:4]),LETTERS),ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
data <- matrix(rnorm(16,0),ncol=4)
data[,1]
data[,2]
data[,3]
data[,4]
The second matrix would generate indices to be used like:
data[,c(1,2)]
data[,c(1,3)]
data[,c(1,4)]
data[,c(2,3)]
etc.
What would a generic, vectorized function look like to accomplish the above for varying n?
Related
So this probably a very newbie question, but let's say I have this matrix
mat <- matrix(rep(c(1,2),3),nrow=6)
[,1]
[1,] 1
[2,] 2
[3,] 1
[4,] 2
[5,] 1
[6,] 2
If I change to the number of columns to 2, the order I get is
matrix(mat,ncol=2)
[,1] [,2]
[1,] 1 2
[2,] 2 1
[3,] 1 2
But I want it to be
[1,] 1 2
[2,] 1 2
[3,] 1 2
I understand that n=col basically splits the matrix in half, and then moves the lower half to the new column. Is there a way to tell R to do in the specific way that I want?
If you use byrow = TRUE, R will assign the elements of the matrix along rows first, giving you what you want:
matrix(mat, ncol = 2, byrow = TRUE)
Output:
[,1] [,2]
[1,] 1 2
[2,] 1 2
[3,] 1 2
We can use
cbind(mat[c(TRUE, FALSE),], mat[c(FALSE, TRUE),])
# [,1] [,2]
#[1,] 1 2
#[2,] 1 2
#[3,] 1 2
I want to create x randomised matrices where only the columns are permuted but the rows are kept constant. I already took a look at permatful() in the vegan package. Nevertheless, i was not able to generate the desired result even though i am quite sure that this should be possible somehow.
df = matrix(c(2,3,1,4,5,1,3,6,2,4,1,3), ncol=3)
This is (one possible) desired result
[,1] [,2] [,3]
[1,] 2 5 2
[2,] 3 1 4
[3,] 1 3 1
[4,] 4 6 3
v
v permutation
v
[,1] [,2] [,3]
[1,] 5 2 2
[2,] 1 4 3
[3,] 3 1 1
[4,] 6 3 4
I tried something like permatfull(df, times=1, fixedmar = "rows", shuffle = "samp") which results in
[,1] [,2] [,3]
[1,] 5 2 2
[2,] 1 4 3
[3,] 3 1 1
[4,] 3 4 6
Now column 1 (originally column 2) has changed from 5,1,3,6 to 5,1,3,3.
Anyone an idea why I do not get the expected result?
Thanks in Advance,
Christian
I am trying to determine which columns were sampled from a matrix randomly sampled within each row. The function sample does not appear to have the ability to tell you which locations were actually sampled. Now, a simple matching routine can solve the problem if all values are unique. However, they are not in my case, so this will not work.
x <- c(2,3,5,1,6,7,2,3,5,6,3,5)
y <- matrix(x,ncol=4,nrow=3)
random <- t(apply(y,1,sample,2,replace=FALSE))
y
[,1] [,2] [,3] [,4]
[1,] 2 1 2 6
[2,] 3 6 3 3
[3,] 5 7 5 5
random
[,1] [,2]
[1,] 2 6
[2,] 3 3
[3,] 5 5
With repeated values in the original matrix, I cannot tell if random[1,1] was sampled from column 1 or column 3, since they both have a value of 2. Hence, matching won't work here.
Accompanying the matrix "random" I would also like a matrix that gives the column from which each value was sampled, in an identically sized matrix. For example, such as:
[,1] [,2]
[1,] 1 4
[2,] 1 3
[3,] 3 4
Thanks!
You need to save your random selections from sample separately so you don't have to worry about matching later. E.g., using y again:
y
# [,1] [,2] [,3] [,4]
#[1,] 2 1 2 6
#[2,] 3 6 3 3
#[3,] 5 7 5 5
set.seed(42)
randkey <- t(replicate(nrow(y),sample(1:ncol(y),2)))
# [,1] [,2]
#[1,] 4 3
#[2,] 2 3
#[3,] 3 2
random <- matrix(y[cbind(c(row(randkey)), c(randkey))], nrow(y))
# [,1] [,2]
#[1,] 6 2
#[2,] 6 3
#[3,] 5 7
I have the matrix y with variable x:
x
[1,] 0
[2,] 1
[3,] 0
[4,] 0
[5,] 1
[6,] 1
I selected just values with 1. Now I have a vector z:
2 5 6
I need match this vector with lines selected with my matrix y. This a example, I have a big data. I tried y[z], but this don't show the rows. Thanks
y[z,] returns matrix y with rows z.
y[z] returns elements z of matrix y
> y <- matrix(1:12, ncol=3)
> y
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> y[c(2,3),]
[,1] [,2] [,3]
[1,] 2 6 10
[2,] 3 7 11
> y[c(2,3)]
[1] 2 3
As Joran points out, if you are working with a single column matrix, include ,drop=FALSE to make sure your output is a matrix.
Let M be the matrix:
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4
[4,] 7 2
I would like to extract all rows with entries equal to the components of the vector
v <- c(3,6,1) from column [,1] in M producing the submatrix m:
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4
I tried
m <- M[which(M[,1] == v), ]
Obtaining the error message longer object length is not a multiple of shorter object length.
Using the transpose t(v) of v does not help.
using %in%:
M[M[,1] %in% v,]
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4