[R] a for loop to lapply
Nick Sabbe
nick.sabbe at ugent.be
Wed Mar 30 09:35:17 CEST 2011
Hello Alex.
A few issues:
* you want seq(dimx) instead of seq(1:dimx) (d'oh)
* I think you have problems with your dimensions in the original code as
well: you use i, which runs up to dimx as an indexer for your third
dimension, of size dimmaps. If dimx > dimmaps, you're in for unexpected
results.
* basic idea of the apply-style functions (nicked *apply below):
- first argument = a collection of items to run over. Could be a
list or a vector
- second argument a function, that could take any of the items in
the collection as its first argument
- other arguments: either tuning parameters (like simplify) for
*apply or passed on as more arguments to the function
- each item from the collection is sequentially fed as the first
argument, the extra arguments (always the same) are also passed to *apply.
- normally, the results of each call are collected into a list,
where the names of the list items refers to your original collection. In
more elaborate versions (sapply) and under some circumstances, this list is
transformed into a simpler structure.
* your test case is rather complicated: I don't think there is a way to make
lapply or one of its cousins to return a threedimensional array just like
that. With sapply (and simplify=TRUE, the default), if the result for each
item of your collection has the same length, the result is coerced into a
twodimensional array with one column for each item in your collection.
* on the other hand, for your example, you probably don't want to use *apply
functions nor loops: it can be done with some clever use of seq and rep and
dim, for sure.
All in all, it seems you may need to get your basics up to speed first, then
shift to *apply (and use a simpler example to get started, like: given a
matrix with two columns, create a vector holding the differences and the
sums of the columns - I know this can be done without *apply as well, but
apart from that it is a more attainable exercise).
Good luck to you on that!
HTH,
Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36
-- Do Not Disapprove
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Alaios
Sent: woensdag 30 maart 2011 8:31
To: R-help at r-project.org
Subject: [R] a for loop to lapply
Dear all,
I am trying to learn lapply.
I would like, as a test case, to try the lapply alternative for the
Shadowlist<-array(data=NA,dim=c(dimx,dimy,dimmaps))
for (i in c(1:dimx)){
Shadowlist[,,i]<-i
}
---so I wrote the following---
returni <-function(i,ShadowMatrix) {ShadowMatrix<-i}
lapply(seq(1:dimx),Shadowlist[,,seq(1:dimx)],returni)
So far I do not get same results with both ways.
Could you please help me understand what might be wrong?
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.
More information about the R-help
mailing list