Dear R developers, Reviewing my code, I have realized that about 80% of the time in the lapply I need to access the names of the objects inside the loop. In such cases I iterate over indexes or names: lapply(names(x), ... [i]), lapply(seq_along(x), ... x[[i]] ... names(x)[i] ), or for(i in seq_along(x)) ... which is rather inconvenient. How about an argument to lapply which would specify the [ or [[ subseting to use in the splitting of the vector? Or may be a different set of functions lapply1, sapply1? I believe this pattern is rather common for other users as well. Thanks. VS.
Michael Lawrence
2010-Sep-21 13:42 UTC
[Rd] lapply version with [ subseting - a suggestion
On Tue, Sep 21, 2010 at 3:55 AM, Vitaly S. <spinuvit.list@gmail.com> wrote:> > Dear R developers, > > Reviewing my code, I have realized that about 80% of the time in the lapply > I > need to access the names of the objects inside the loop. > > In such cases I iterate over indexes or names: > lapply(names(x), ... [i]), > lapply(seq_along(x), ... x[[i]] ... names(x)[i] ), or > for(i in seq_along(x)) ... > > which is rather inconvenient. > > How about an argument to lapply which would specify the [ or [[ subseting > to use > in the splitting of the vector? > Or may be a different set of functions lapply1, > sapply1? > >I'm not sure what you want exactly, but what about just using mapply over the names and vector elements?> I believe this pattern is rather common for other users as well. > > Thanks. > VS. > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Sep-21 13:47 UTC
[Rd] lapply version with [ subseting - a suggestion
See ?vapply also On Tue, Sep 21, 2010 at 7:55 AM, Vitaly S. <spinuvit.list@gmail.com> wrote:> > Dear R developers, > > Reviewing my code, I have realized that about 80% of the time in the lapply > I > need to access the names of the objects inside the loop. > > In such cases I iterate over indexes or names: > lapply(names(x), ... [i]), > lapply(seq_along(x), ... x[[i]] ... names(x)[i] ), or > for(i in seq_along(x)) ... > > which is rather inconvenient. > > How about an argument to lapply which would specify the [ or [[ subseting > to use > in the splitting of the vector? > Or may be a different set of functions lapply1, > sapply1? > > I believe this pattern is rather common for other users as well. > > Thanks. > VS. > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Reasonably Related Threads
- Applying ifelse() on different functions
- lapply (and friends) with data.frames are slow
- for loop; lm() regressions; list of vectors - lapply - accolades and square brackets??
- recursive lapply and keeping track of data
- lapply or data.table to find a unit's previous transaction