[R] a for loop to lapply

Andreas Borg andreas.borg at unimedizin-mainz.de
Wed Mar 30 09:32:37 CEST 2011


Hi Alaios,

there are some problems with your code:

1. In the expression "Shadowlist[,,i]<-i", i corresponds to dimmaps, not 
dimx. The for loop should be either

for (i in c(1:dimmaps)){
    Shadowlist[,,i]<-i
}

or

for (i in c(1:dimx)){
    Shadowlist[i,,]<-i
}


2. in lapply, the function must be second argument, your expression

lapply(seq(1:dimx),Shadowlist[,,seq(1:dimx)],returni)


fails because lapply wants to call Shadowlist[...] as a function. You 
probably meant (including the correction above):

lapply(seq(1:dimmaps),returni,Shadowlist[,,seq(1:dimmaps)])

or

lapply(seq(1:dimx),returni,Shadowlist[seq(1:dimx),,])

3. lapply always returns a list, there is no way to make it return an array. apply is better to deal with arrays, but as far as I know it cannot return arrays with dimension greater than 2.

Finally a general remark for future posts: Please make sure your examples are self-contained and executable (i.e. they don't throw an error and they do not depend on undefined variables, such as dimx etc. in this case)

Best regards,

Andreas



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



More information about the R-help mailing list