Reputation: 67
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?
Upvotes: 0
Views: 437
Reputation: 887971
Or another option is replace
replace(m, cbind(seq_len(nrow(m)), 4), m[,4] - 5)
m <- matrix(c(1,2,3,4),4,4, byrow = TRUE)
Upvotes: 0
Reputation: 4233
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
Upvotes: 2