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
On Thu, May 20, 2004 at 03:52:22PM -0500, Kenneth Cabrera wrote:> 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?There's still a better way to do it, I'm sure, but something like... ##untested! myMerge <- function(x,y,...) { zz <- merge(x,y,...) gc() } foo <- lapply(my.df.list[[-1]],myMerge,my.df.list[[1]]) Cheers Jason -- Indigo Industrial Controls Ltd. http://www.indigoindustrial.co.nz 64-21-343-545 jasont at indigoindustrial.co.nz
> 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 about: a <- list( data.frame(a=1), data.frame(a=2), data.frame(a=3), data.frame(a=4) ) do.call("rbind", a) do.call("rbind", a[3:4]) (assuming that the data frames have the same variables) Hadley