# [R] mapply to lapply

Alaios alaios at yahoo.com
Mon Apr 18 13:10:13 CEST 2011

```Dear Andreas,
I would like to thank you for your reply.
I have tried two alternatives but none of the two worked out:

F2[i+1,j+1]<-sum(lapply(1:nrow(cells), function(rowInd) Fwithcellvalue(i=i,j=j,a=cells[rowInd,2],b=cells[rowInd,4],c=cells[rowInd,1],d=cells[rowInd,3],e=cells[rowInd,5])))

this one is executed : takes like 2 mins to return(which is normal) but returns the following

^@Error in sum(lapply(1:nrow(cells), function(rowInd) Fwithcellvalue(i = i,  :
invalid 'type' (list) of argument

afterwards I tried to change the function definition so to pass i,j inside:

This one does not execute at all

F2[i+1,j+1]<-sum(lapply(1:nrow(cells), function(rowInd,i,j) Fwithcellvalue(i=i,j=j,a=cells[rowInd,2],b=cells[rowInd,4],c=cells[rowInd,1],d=cells[rowInd,3],e=cells[rowInd,5])))
Error in paste("f", i, j, "(a,b,c,d)", sep = "") :
argument "i" is missing, with no default

What do you think I should try out now?

Vielen Dank
Alex

--- On Mon, 4/18/11, Andreas Borg <andreas.borg at unimedizin-mainz.de> wrote:

> From: Andreas Borg <andreas.borg at unimedizin-mainz.de>
> Subject: Re: [R] mapply to lapply
> To: "Alaios" <alaios at yahoo.com>
> Cc: R-help at r-project.org
> Date: Monday, April 18, 2011, 11:10 AM
> My solution would be to use an index
> variable that goes from 1 to the number of rows that are to
> be processed, along with a helper function which calls
> Fwithcellvalue with the suitable arguments:
>
> F2[i+1,j+1]<-sum(lapply(1:nrow(cells), function(rowInd)
> Fwithcellvalue(i=i,j=j,a=cells[rowInd,2],b=cells[rowInd,4],c=cells[rowInd,1],d=cells[rowInd,3],e=cells[rowInd,5]))
>
>
> Best regards,
>
> Andreas
>
> Alaios schrieb:
> > Dear all,
> >
> > I would like to ask your help concerning converting a
> mapply function to lapply. The reason is that I would like
> to use mclapply which requires lapply syntax.
> >
> > The command I would like to convert is:
> >
> F2[i+1,j+1]<-sum(mapply(Fwithcellvalue,i=i,j=j,a=cells[,2],b=cells[,4],c=cells[,1],d=cells[,3],e=cells[,5]))
> >
> > Could you please help me understand how I should
> change it?
> >
> >
> > Best Regards
> > Alex
> >
> > ______________________________________________
> > R-help at r-project.org
> 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.
> >
> >
>
>
> -- Andreas Borg
> Medizinische Informatik
>
> UNIVERSITÄTSMEDIZIN
> der Johannes Gutenberg-Universität
> Institut für Medizinische Biometrie, Epidemiologie und
> Informatik
> Obere Zahlbacher Straße 69, 55131 Mainz
> www.imbei.uni-mainz.de
>
> Telefon +49 (0) 6131 175062
> E-Mail: borg at imbei.uni-mainz.de
>
> Diese E-Mail enthält vertrauliche und/oder rechtlich
> geschützte Informationen. Wenn Sie nicht der
> richtige Adressat sind oder diese E-Mail irrtümlich
> erhalten haben, informieren Sie bitte sofort den
> Absender und löschen Sie diese Mail. Das unerlaubte
> Kopieren sowie die unbefugte Weitergabe
> dieser Mail und der darin enthaltenen Informationen ist
> nicht gestattet.
>
>

```