Returning row number using sapply

Viewed 352

I am trying to start moving away from loops in R, but am having trouble figuring out how to return information on the progress of the sapply function. For example, if I want to process a vector and print out what row I am processing, using a loop I write:

vec = c(1:10)
out = NULL
for (i in 1:length(vec)){
    print(paste("Processing item ",i,sep=""))
    y = vec[i]^2
    out = c(out,y)
}

How can I do the same thing with sapply? Here is the code I have.

func = function(x) {
    #print (paste("Processing item ",x,sep="")) ## This is where I want to print out the row number being processed.
    x^2
}

out = sapply(vec,func)

Thanks for any information.

3 Answers
Related