lapply version with [ subseting - a suggestion

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

lapply version with [ subseting - a suggestion

Vitalie S.-2

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.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: lapply version with [ subseting - a suggestion

Michael Lawrence-3
On Tue, Sep 21, 2010 at 3:55 AM, Vitaly S. <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: lapply version with [ subseting - a suggestion

Henrique Dallazuanna
In reply to this post by Vitalie S.-2
See ?vapply also

On Tue, Sep 21, 2010 at 7:55 AM, Vitaly S. <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: lapply version with [ subseting - a suggestion

Vitalie S.-2
In reply to this post by Michael Lawrence-3
>> 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?
>

General rule for "[[" operator is to extract the atomic part of the object.
"[" instead returns an object of the same type as original.

Just imagine an S4 objects with multiple parallel vectors as slots. My "["
extracts an object of the same class as the original, but now, in each slot
is an element with length one.

With current tools in R one can not loop through such an object easily. If a
version of "lapply" would exist which would split the original object with
"[" the problem would be solved.

Thanks for the tip on mapply, works for the names indeed. Never occurred to
me, ashamed.

>
>
>> I believe this pattern is rather common for other users as well.
>>
>> Thanks.
>> VS.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel