Skip to content
Prev 274683 / 398506 Next

how to use 'which' inside of 'apply'?

Your original code works far faster when the input
is a matrix that when it is a data.frame.  Selecting
a row from a data.frame is a very slow operation,
selecting a row from a matrix is quick.  Modifying
a row or a single element in a data.frame is even
worse compared to do it on a matrix.  I compared your
original code:

f0 <- function (df)
{
    for (i in seq_len(nrow(df))) {
        t = which(df[i, 2:24] > df[i, 25])
        r = min(t)
        df[i, 26] = (r - 1) * 16 + 1
    }
    df[, 26] # for now just return the computed column
}

to one that converts relevant parts of the data.frame
df to matrices or vectors before doing the loop over
rows:

f0.a <- function (df)
{
    thold <- df[, "thold"]
    tmp <- as.matrix(df[,2:24])
    ans <- df[,26]
    for (i in seq_len(nrow(df))) {
        t = which(tmp[i,]>thold[i])
        r = min(t)
        ans[i] = (r-1)*16+1
    }
    # df[,26] <- ans
    ans
}

On a 10,000 row data.frame f0 took 47.950 seconds and f0.a took
0.140 seconds.  (f1, below, took 0.012 seconds.)



Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com