R: How to subtract every n-th column from the ones before it in a matrix/data-frame?
let's say I have a matrix like this:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 3 5 7 9 11 13 15 17
[2,] 开发者_运维技巧2 4 6 8 10 12 14 16 18
I'm looking for an easy way to substract the 3rd column from the 1st and 2nd, then the 6th from the 4th and 5th, and so on.
Can I do this without a for-loop?
Thanks in advance, zenzen.
This answer isn't beautiful. It should really be made into a function for clarity, but:
m <- matrix(1:18,nrow=5,ncol=9, byrow=TRUE)
colsA <- (1:ncol(m))[1:ncol(m)%%3!=0]
colsB <- (1:ncol(m))[1:ncol(m)%%3==0]
m[,colsA] <- m[,colsA] - m[,rep(colsB,each=2)]
no for loop! And the result is:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] -2 -1 3 -2 -1 6 -2 -1 9
[2,] -2 -1 12 -2 -1 15 -2 -1 18
edit: here it is as a function
nth <- function(x,n) {
colsA <- (1:ncol(x))[1:ncol(x)%%n!=0]
colsB <- rep((1:ncol(x))[1:ncol(x)%%n==0], each=n-1)
x[,colsA] <- x[,colsA] - x[,colsB]
x
}
Here is one way.
First I show the principle:
x <- matrix(1:20, nrow=2)
x[, seq(1, 7, 3)] <- x[, seq(1, 7, 3)] - x[, seq(3, 9, 3)]
x[, seq(2, 8, 3)] <- x[, seq(2, 8, 3)] - x[, seq(3, 9, 3)]
x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] -4 -2 5 -4 -2 11 -4 -2 17 19
[2,] -4 -2 6 -4 -2 12 -4 -2 18 20
And next I define a helper function that make a little less typing:
myseq <- function(start, object=x){
seq(start, 3 * (ncol(x) %/% 3), 3)
}
x <- matrix(1:20, nrow=2)
x[, myseq(1)] <- x[, myseq(1)] - x[, myseq(3)]
x[, myseq(2)] <- x[, myseq(2)] - x[, myseq(3)]
x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] -4 -2 5 -4 -2 11 -4 -2 17 19
[2,] -4 -2 6 -4 -2 12 -4 -2 18 20
your_seq <- seq(from=3, to=ncol(your_matrix), by=3)
for(x in 1:length(your_seq)) {
col1 <- your_seq[x] - 1
col2 <- your_seq[x] - 2
your_matrix[,c(col1,col2)] <- your_matrix[,c(col1,col2)] - your_matrix[,your_seq[x]]
}
精彩评论