[R] do.call vs. lapply for lists

jim holtman jholtman at gmail.com
Mon Apr 9 18:58:25 CEST 2007


On 4/9/07, Muenchen, Robert A (Bob) <muenchen at utk.edu> wrote:
> Hi All,
>
> I'm trying to understand the difference between do.call and lapply for
> applying a function to a list. Below is one of the variations of
> programs (by Marc Schwartz) discussed here recently to select the first
> and last n observations per group.
>
> I've looked in several books, the R FAQ and searched the archives, but I
> can't find enough to figure out why lapply doesn't do what do.call does
> in this case. The help files & newsletter descriptions of do.call sound
> like it would do the same thing, but I'm sure that's due to my lack of
> understanding about their specific terminology. I would appreciate it if
> you could take a moment to enlighten me.
>
> Thanks,
> Bob
>
> mydata <- data.frame(
>  id      = c('001','001','001','002','003','003'),
>  math    = c(80,75,70,65,65,70),
>  reading = c(65,70,88,NA,90,NA)
> )
> mydata
>
> mylast <- lapply( split(mydata,mydata$id), tail, n=1)
> mylast
> class(mylast) #It's a list, so lapply will so *something* with it.
>
> #This gets the desired result:
> do.call("rbind", mylast)
This is doing a single 'rbind' with the elements of the list as the
parameters so you are effectively creating a single data frame from
it.
>
> #This doesn't do the same thing, which confuses me:
> lapply(mylast,rbind)
This is applying 'rbind' separately to each element of the list (that
is what lapply does - call the function with each element) and will
return a list which is exactly the same.
>
> #...and data.frame won't fix it as I've seen it do in other
> circumstances:
> data.frame( lapply(mylast,rbind) )
What you are effectively doing is calling data.frame with as many
parameters as you have elements of the list.  See what happens with:

> data.frame(a=list(a=1,b=2), b=list(a=3,b=4))
  a.a a.b b.a b.b
1   1   2   3   4

>
> =========================================================
>  Bob Muenchen (pronounced Min'-chen), Manager
>  Statistical Consulting Center
>  U of TN Office of Information Technology
>  200 Stokely Management Center, Knoxville, TN 37996-0520
>  Voice: (865) 974-5230
>  FAX:   (865) 974-4810
>  Email: muenchen at utk.edu
>  Web:   http://oit.utk.edu/scc,
>  News:  http://listserv.utk.edu/archives/statnews.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list