Subtract vector from one column of a matrix - r

I'm a complete R novice, and I'm really struggling on this problem. I need to take a vector, evens, and subtract it from the first column of a matrix, top_secret. I tried to call up only that column using top_secret[,1] and subtract the vector from that, but then it only returns the column. Is there a way to do this inside the matrix so that I can continue to manipulate the matrix without creating a bunch of separate columns?

Sure, you can. Here is an example:
m <- matrix(c(1,2,3,4),4,4, byrow = TRUE)
> m
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
[4,] 1 2 3 4
m[,4] <- m[,4] - c(5,5,5,5)
which gives:
> m
[,1] [,2] [,3] [,4]
[1,] 1 2 3 -1
[2,] 1 2 3 -1
[3,] 1 2 3 -1
[4,] 1 2 3 -1

Or another option is replace
replace(m, cbind(seq_len(nrow(m)), 4), m[,4] - 5)
data
m <- matrix(c(1,2,3,4),4,4, byrow = TRUE)

Related

Subsettings rows containing specific values

I am generating a matrix of all combinations of 5 numbers taken 3 at a time, without replacement, like this:
v <- seq(1,5,1)
combs <- t(combn(v,3))
Part of the output is as following:
[,1] [,2] [,3]
[,1] 1 2 3
[,2] 1 2 4
[,3] 1 2 5
[,4] 1 3 4
[,5] 1 3 5
.
.
.
Now, I want to filter out all rows containing, for example, numbers 1 and 3, where the remaining element doesn't matter.
How can this be done?
Thank you
Here is one way using rowSums :
combs[rowSums(combs == 1) > 0 & rowSums(combs == 3) > 0, ]
# [,1] [,2] [,3]
#[1,] 1 2 3
#[2,] 1 3 4
#[3,] 1 3 5
You can also use apply :
combs[apply(combs, 1, function(x) all(c(1, 3) %in% x)), ]

Calculations within a matrix with internal, references that are anchored to columns within the matrix in R

I have a matrix and I would like to perform a calculation on each number in the matrix so that I get another matrix with the same dimensions only with the results of the calculation. This should be easy except that part of the equation is dependent on which column I am accessing because I will need to have an internal reference to the number at row [3,] within that column.
The equation I would like to apply is:
output matrix value = input_matrix value at a given position + (1- (matrix value at [3,] and in the same column as the input matrix value))
For example, For (1,1) in the matrix the calculation would be 1+(1-3)
For position (1,2) in the matrix, the calculation would be 5+(1-7)
input_matrix<- matrix(1:12, nrow = 4, ncol = 3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
The output matrix should end up looking like this:
[,1] [,2] [,3]
[1,] -1 -1 -1
[2,] 0 0 0
[3,] 1 1 1
[4,] 2 2 2
I have tried doing something like this:
output_matrix<-apply(input_matrix,c(1,2), function(x) x+(1-(input_matrix[3,])))
but that gives me three matrices with the wrong dimensions as the output.
I am thinking that perhaps I can perhaps just modify the function in the above calculation to get this to work, or alternatively write something that iterates over each column of the matrix but I am not sure exactly how to do this in a way that gives me the output matrix that I want.
Any help would be greatly appreciated.
I think this should work for you:
apply(input_matrix, margin = 2, function(x) x + (1 - x[3]))
[,1] [,2] [,3]
[1,] -1 -1 -1
[2,] 0 0 0
[3,] 1 1 1
[4,] 2 2 2
We could also do this in a vectorized way
input_matrix + (1 - input_matrix[3,][col(input_matrix)])
# [,1] [,2] [,3]
#[1,] -1 -1 -1
#[2,] 0 0 0
#[3,] 1 1 1
#[4,] 2 2 2

Repeat rows in a data frame according to a vector

I have a data frame and would like to repeat each row by each element in a pre defined vector.
for example if I have a matrix (I use matrix for example)
matrix(c(1,2,3,2,1,3),2)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 2 3
I would like this to return
matrix(c(1,1,2,2,3,3,2,2,1,1,3,3),4)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 1 3 1
[3,] 2 2 3
[4,] 2 2 3
if the vector was vec = c(2,2).
my vector has varying size elements. Sorry, I am new to coding.
Repeat over the row numbers. In your example:
base = matrix(c(1,2,3,2,1,3),2)
rows = 1:nrow(base)
index= rep(rows, c(2,2))
base[index,]

R indexing issue

Sorry for vague question title, i couldn't figure out something more specific.
I have 3x2 matrix c:
> c
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 2 3
It is important that ncol(c) == 2.
I also have matrix ind:
> ind
[,1] [2] [,3] [,4]
[1,] 2 2 2 1
[2,] 1 1 2 2
[3,] 2 2 2 1
It is important that nrow(c) == nrow(ind), and that the values of matrix ind are 1 and 2 (like column indices for each row of c)
What i want to get is matrix a with same dim as ind such that a[i,j] == c[i,ind[i,j]]:
> a
[,1] [2] [,3] [,4]
[1,] 2 2 2 1
[2,] 1 1 3 3
[3,] 3 3 3 2
I can do something similar in less comprehensive situations, for example if nrow(c) == 1 i'll use apply:
> apply(c,2,function(x){return(matrix(x[ind], nrow(ind)))})
I know there is a way to iterate by 2 lists using mapply, but
1) i don't know what's the best way to represent matrix as list of rows
2) i fing this solution ugly
What is the best way to achieve what i descibed here?
Matrix indexing to the rescue!
> c.mat <- matrix(c(1,1,2,2,3,3), ncol=2)
> ind <- matrix(c(2,1,2,2,1,2,2,2,2,1,2,1), ncol=4)
> matrix(c.mat[cbind(as.vector(row(ind)), as.vector(ind))], ncol=ncol(ind))
[,1] [,2] [,3] [,4]
[1,] 2 2 2 1
[2,] 1 1 3 3
[3,] 3 3 3 2
f<-function(x,row1){
for(i in 1:length(x)){
x[i]=cc[i,ind[i,row1]]
}
x
}
a=apply(cc,1,f,nrow(a))
You can use apply like this. Note: cc is your c matrix

Form matrix from rows in 3-dimensional array

I have X, a three-dimensional array in R. I want to take a vector of indices indx (length equal to dim(X)[1]) and form a matrix where the first row is the first row of X[ , , indx[1]], the second row is the second row of X[ , , indx[2]], and so on.
For example, I have:
R> X <- array(1:18, dim = c(3, 2, 3))
R> X
, , 1
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
, , 2
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
, , 3
[,1] [,2]
[1,] 13 16
[2,] 14 17
[3,] 15 18
R> indx <- c(2, 3, 1)
My desired output is
R> rbind(X[1, , 2], X[2, , 3], X[3, , 1])
[,1] [,2]
[1,] 7 10
[2,] 14 17
[3,] 3 6
As of now I'm using the inelegant (and slow) sapply(1:dim(X)[2], function(x) X[cbind(1:3, x, indx)]). Is there any way to do this using the built-in indexing functions? I had no luck experimenting with the matrix indexing methods described in ?Extract, but I may just be doing it wrong.
Maybe like this:
t(sapply(1:3,function(x) X[,,idx][x,,x]))
I may be answering the wrong question (I can't reconcile your first description and your sample output)... This produces your sample output, but I can't say that it's much faster without running it on your data.
do.call(rbind, lapply(1:dim(X)[1], function(i) X[i, , indx[i]]))
Matrix indexing to the rescue! No applys needed.
Figure out which indices you want:
n <- dim(X)[2]
foo <- cbind(rep(seq_along(indx),n),
rep(seq.int(n), each=length(indx)),
rep(indx,n))
(the result is this)
[,1] [,2] [,3]
[1,] 1 1 2
[2,] 2 1 3
[3,] 3 1 1
[4,] 1 2 2
[5,] 2 2 3
[6,] 3 2 1
and use it as index, converting back to a matrix to make it look like your output.
> matrix(X[foo],ncol=n)
[,1] [,2]
[1,] 7 10
[2,] 14 17
[3,] 3 6

Resources