[R] for() to lapply()

Kenneth Cabrera krcabrer at perseus.unalmed.edu.co
Thu May 20 22:52:22 CEST 2004


Hi dear R-users:

I have the following problem:
I have a list of data.frames (12 variables and 60000 rows, each)

I have to merge from an specific point of the list to the
end of the list, I am doing so with a for() loop but it is
too inefficient and it exhausts memory.

How can I convert this for() loop in a function and then use
lapply?
-------------------------------------------------------------
LIST: list of data frames.
m:point of start merging.

result<-merge(LIST[[m]],LIST[[m+1]],by="key",all.x=T)
for (i in (m+2):length(LIST))
{
   result<-merge(result,LIST[[i]],by="key",all.x=T)
}

The problem is the acumulative: "result<-merge(result,...)"

I can think in a function like this:

mergeListelem<-function(i,LS,m)
{
  merge(LS[[m]],LS[[i]],by="key",all.x=T)
}

ind<-m:length(LIST)
lapply(ind,mergeListelem,LIST,m)

But I just obtain a list of merged data.frames,
and I donÂ´t know how to join all the elements
of the list in just one final data.frame.

Thank you for your help

-- 
Kenneth Cabrera
Universidad Nacional de Colombia
Tel Of 430 9351
Cel 315 504 9339
MedellÃ­n




More information about the R-help mailing list