Skip to content

using apply to loop

2 messages · Louis Martin, Tyler Smith

#
On 2007-12-21, Louis Martin <louismartinbis at yahoo.fr> wrote:
Off the top, data is a bad choice for your dataframe, as it conflicts
with a standard function. Also, including some actual data would make
this easier to work with. I think you're using dim(data)[[1]] to get
the number of rows of data? That can be more clearly expressed as
nrow(data), and dim(data)[[2]] == ncol(data).

Anyways, this might be helpful:

add.mat <- apply(data[,1:nclass], MAR = 1, FUN = function(x) 
	         ifelse(x == max(x), 1, 0))

for(i in 1:n)
    z[ , data[i, ncol(data)]] <- z[ , data[i, ncol(data)]] + add.mat[,i]

There's still a loop, but it might not be needed depending on what
values 'truth' holds. Most of the calculations are shifted into the
apply() call, so the one line loop should run at least a little faster
than what you started with.

HTH,

Tyler