[R] adding rows

arun smartpink111 at yahoo.com
Sat May 10 12:55:29 CEST 2014



HI,
If you want to try other ways:

fun1 <- function(mat, rowN) {
    dm <- dim(mat)[1]
    rowN1 <- rowN - 1
    indx <- rep(1:rowN, dm - rowN1) + rep(seq(0, dm - rowN), each = rowN)
    indx1 <- (seq_along(indx)-1)%/%rowN+1
    as.vector(tapply(indx, list(indx1), FUN = function(i) sum(mat[i, ])))
}

#or

fun2 <- function(mat, rowN) {
    dm <- dim(mat)[1]
    rowN1 <- rowN - 1
    indx <- rep(1:rowN, dm - rowN1) + rep(seq(0, dm - rowN), each = rowN)
    mat1 <- mat[indx, ]
    indx1 <- rep((seq_along(indx) - 1)%/%rowN + 1, ncol(mat))
    colSums(matrix(mat1[sort.int(indx1, method = "quick", index.return = TRUE)$ix], 
        ncol = dm - rowN1))
}

##But the above methods are slower in large datasets.  
##Rui's function

funOld <- function(mat, rowN) {
    dm <- dim(mat)[1]
    rowN1 <- rowN - 1
    sapply(1:(dm - rowN1), function(i) sum(mat[i:(i + rowN1), ]))
}

###Modified Rui's function using ?for() loop instead of ?sapply()

funNew <- function(mat, rowN) {
    dm <- dim(mat)[1]
    rowN1 <- rowN - 1
    vec <- vector(mode = "numeric", length = dm - rowN1)
    for (i in 1:(dm - rowN1)) {
        vec[i] <- sum(mat[i:(i + rowN1), ])
    }
    vec
}

##Speed comparison
set.seed(348)
el1 <- matrix(sample(1:300, 1e6*50,replace=TRUE),ncol=50)

system.time(r1 <- fun1(el1,3))
#  user  system elapsed 
# 15.888   0.120  16.042 

system.time(r2 <- funOld(el1,3))
#   user  system elapsed 
#  5.061   0.004   5.076 

 system.time(r3 <- fun2(el1,3))
 #  user  system elapsed 
 #12.371   1.329  13.735 

system.time(r4 <- funNew(el1,3))
#   user  system elapsed 
#  4.716   0.000   4.727 

library(compiler)
funOldc <- cmpfun(funOld)
funNewc <- cmpfun(funNew)
fun2c <- cmpfun(fun2)
 system.time(r5 <- funOldc(el1,3))
 #  user  system elapsed 
 # 7.458   0.000   7.476 

system.time(r6 <- funNewc(el1,3))
 #  user  system elapsed 
 # 3.529   0.000   3.536 
 sapply(paste0("r",2:6),function(x) all.equal(r1,get(x) ))
 # r2   r3   r4   r5   r6 
#TRUE TRUE TRUE TRUE TRUE 


A.K.







On Friday, May 9, 2014 5:56 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
Hello,

Try the following.

sapply(1:(30 - 2), function(i) sum(el[i:(i+2), ]))


but with number of rows instead of 30.

Hope this helps,

Rui Barradas

Em 09-05-2014 22:35, eliza botto escreveu:
> Dear useRs,
> I have a matrix, say "el" of 30 rows and 10 columns, as
> el<-matrix(sample(1:300),ncol=10)
> I want to sum up various sets of three rows of each column in the following manner
> sum(el[c(1,2,3),]) ##adding row number 1, 2 and 3 of each column
> sum(el[c(2,3,4),])##adding row number 2, 3 and 4 of each column
> sum(el[c(3,4,5),])##adding row number 3, 4 and 5 of each column
> sum(el[c(4,5,6),])
> sum(el[c(5,6,7),])
> sum(el[c(6,7,8),])
> sum(el[c(7,8,9),])
> sum(el[c(8,9,10),])
> sum(el[c(9,10,11),])
> ................
> ......
> so on
> ..............
> I know how to do it manually, but since my original matrix has 2000 rows, I therefore want to figure out a more conveinient way.
> Thankyou so very much in advance,
>
> Eliza                         
>     [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

>

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list