Re: [R] mapply to lapply

From: Andreas Borg <andreas.borg_at_unimedizin-mainz.de>
Date: Mon, 18 Apr 2011 15:54:45 +0200

Hi Alex,

sorry, I wasn't aware that sum does not work with listst. replace "lapply" with "sapply" or place the call in "unlist".

Your second example does not work as you have to provide the additional arguments to the inner function in lapply:

lapply(1:nrow(cells), function(rowInd, i, j), [your function goes here], i=i, j=j)

Viele Grüße,

Andreas

Alaios schrieb:
> 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.
>>
>>
>>
>
>

-- 
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.

______________________________________________
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.
Received on Mon 18 Apr 2011 - 13:55:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Apr 2011 - 14:20:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive