[R] memory growth with rbind and lapply

Duncan Murdoch murdoch.duncan at gmail.com
Sun May 13 10:38:20 CEST 2012


On 12-05-12 10:06 PM, Jack Tanner wrote:
> This version of my code makes the R process consume unreasonable amounts of RAM:
>
>    datf<- rbind(lapply(mylist, function(item) {
>        with(item, data.frame(col1, col2, col3))
>      }))
>
> This version works fine:
>
>    datf<- lapply(mylist, function(item) {
>        with(item, data.frame(col1, col2, col3))
>      })
>    datf<- do.call(rbind, datf)
>
> Is this to be expected?

They are different, so I'd expect differences:

 > x <- list(data.frame(a=1), data.frame(a=2),data.frame(a=3))
 > rbind(x)
   [,1]   [,2]   [,3]
x List,1 List,1 List,1
 > do.call(rbind, x)
   a
1 1
2 2
3 3

The first doesn't really make sense, so the fact that it takes a lot of 
memory may be R trying too hard to do something sensible.

Duncan Murdoch



More information about the R-help mailing list