Re: [R] how to make the code more efficient using lapply

2018-05-25 Thread Bert Gunter
... and, of course, the original premise is false. apply() type statements **are** loops at the interpreter level and are typically not appreciably faster -- sometimes even a bit slower -- than explicit for() loops. Their chief advantage is adherence to a functional programming paradigm that for ma

Re: [R] how to make the code more efficient using lapply

2018-05-25 Thread MacQueen, Don via R-help
Eric's approach seems reasonable to me, and I agree that it's probably not the use of a "for" loop that makes the original version slow. As Eric mentioned, there are lots of unnecessary things happening in the loop. For example, list.files() was called twice inside the loop, which is unnecessar

Re: [R] how to make the code more efficient using lapply

2018-05-25 Thread Eric Berger
Hi Stephen, I am not sure that the "for loop" is the source of slowness. You seem to be doing a lot of unnecessary work each time through the loop. e.g. no need to check if it's the last file, just move that section outside of the loop. It will be executed when the loop finishes. As it is you are c

[R] how to make the code more efficient using lapply

2018-05-24 Thread Stephen HonKit Wong
Dear All, I have a following for-loop code which is basically intended to read in many excel files (each file has many columns and rows) in a directory and extract the some rows and columns out of each file and then combine them together into a dataframe. I use for loop which can do the work but q