[R] spped up a function

David Winsemius dwinsemius at comcast.net
Tue Jul 2 20:24:28 CEST 2013


On Jul 2, 2013, at 10:47 AM, Santiago Guallar wrote:

> Hi,
> 
> I have written a function to assign the values of a certain variable 'wd' from a dataset to another dataset. Both contain data from the same time period but differ in the length of their time intervals: 'GPS' has regular 10-minute intervals whereas 'xact' has irregular intervals. I attached simplified text versions from write.table. You can also get a dput of 'xact' in this address: http://www.megafileupload.com/en/file/431569/xact-dput.html).
> The original objects are large and the function takes almost one hour to finish.
> Here's the function:
> 
> fxG= function(xact, GPS){
> l <- rep( 'A', nrow(GPS) )
> v <- unique(GPS$Ring) # the process is carried out for several individuals identified by 'Ring'
> for(k in v ){
>    
>    df <- xact[xact$Ring == v,]

Simplified a bit , this is starting to look like a case for the split function:

>    for(i in 1:nrow(GPS)){
>          if(GPS[i,]$Ring== v){# the code runs along the whole data.frame for each i;

                   # After doing the simplification I must ask how GPS[i,]$Ring could not == v ( or I)

                      
> 
>           u <- df$timepos <= GPS[i,]$timepos
>                              # fill vector l for each interval t from xact <= each interval from GPS (take the max if there's > 1 interval)
>           l[i] <- df[max( which(u == TRUE) ),]$wd

                 #perhaps tail(df[which(u), 'wd'],1)?
>                               }
>                         }
>                      }
> return(l)}
> 
This looks like it will be overwriting the l-object with every iteration of 'k'

> vwd <- fxG(xact, GPS)
> 
> 
> My question is: how can I speed up (optimize) this function?

The first thing you should do is describe in natural language what is desired to be done with objects: 'xact' and 'GPS' not yet described .... rather than asking for simplification of obscure nested  for-loops with probably redundant assignments and extraneous conditions. Make a simple example of such objects and repost.

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list