[R] Give me all operator

Joshua Wiley jwiley.psych at gmail.com
Wed Nov 17 16:53:54 CET 2010


MyStruct <- list(list(CRmap = rnorm(50), xy = 1:50), list(CRmap =
rnorm(50), xy = 51:100))

I wonder if it would be possible to extend indexing to accept lists.
For example:

MyStruct[[c(1, 2)]] extracts level1[1] and level2[2], so if the vector
was a list, perhaps
MyStruct[[list(c(1, 2), 2)]] ## level1[1:2], level2[2]

In any case, I do not see a way presently that avoids lappy, but
within that limitation, this is slightly simpler:

lapply(MyStruct, `[`, "xy")


Josh


On Wed, Nov 17, 2010 at 6:56 AM, Ivan Calandra
<ivan.calandra at uni-hamburg.de> wrote:
> I understand now!
> But I don't know of any easy way to do it.
>
> You would expect that something like these would work:
> Mystruct[[1:2]][["xy"]]  ## but error: subscript out of bounds, which makes
> sense because "[[" accept only a single subscript
> Mystruct[1:2][["xy"]]     ## but NULL, which makes sense because "[" keeps
> the upper elements, not only what is within Mystruct[1] and Mystruct[2] (not
> sure I've stated right, but I hope you understand).
>
> I usually prefer using "[[" or "[" instead of "$" because you can use
> indexes, which is better to vectorize or loop.
>
> So the only solution I've found (maybe someone here might give us a better
> answer) is:
> lapply(Mystruct, FUN=subset, select=xy)
> or
> lapply(Mystruct, FUN=subset, subset=whatever, select=xy)
> if you also want to subset some specific rows
>
> HTH,
> Ivan
>
>
>
> Le 11/17/2010 15:26, Alaios a écrit :
>>
>> It seems that I am confusing something:
>>
>> List of 50
>>  $ :List of 2
>>  ..$ CRmap: logi [1:100, 1:100] NA NA NA NA NA NA ...
>>  ..$ xy   : num [1:2] 21 11
>>  $ :List of 2
>>  ..$ CRmap: logi [1:100, 1:100] NA NA NA NA NA NA ...
>>  ..$ xy   : num [1:2] 80 68
>>
>>
>>
>> --- On *Wed, 11/17/10, Ivan Calandra /<ivan.calandra at uni-hamburg.de>/*
>> wrote:
>>
>>
>>    From: Ivan Calandra <ivan.calandra at uni-hamburg.de>
>>    Subject: Re: [R] Give me all operator
>>    To: r-help at r-project.org
>>    Date: Wednesday, November 17, 2010, 2:05 PM
>>
>>    Well, without knowing what is the structure of Mystruct.Map and
>>    Mystruct.xy, it's really difficult to know how to help you.
>>    And what is Mystruct by the way!?
>>    Could you send us the output from dput(your_objects) and/or
>>    str(your_objects)?
>>
>>    I think you're really confused about the structure of your
>>    objects. You
>>    probably need to read more stuff about that!
>>
>>    Ivan
>>
>>    Le 11/17/2010 14:56, Alaios a écrit :
>>    > Thanks a lot for your helpful answer.
>>    > In my case now I have implemented some "struct" with the following
>>    > structure
>>    > Mystruct.Map
>>    > Mystruct.xy
>>    >
>>    > If I do Mystruct[2].$xy I get correctly the xy values of the
>>    second item.
>>    >
>>    > I want now to print all the $xy fields of the Mystruct[[]]
>>    > I tried
>>    > Mystruct[data.frame(a=1:5)]]$xy which returns the following message:
>>    >
>>    > invalid subscript type 'list'
>>    >
>>    >
>>    > I would like to thank you again for your support
>>    > Regards
>>    > Alex
>>    >
>>    > --- On *Wed, 11/17/10, Ivan Calandra
>>    /<ivan.calandra at uni-hamburg.de
>>    </mc/compose?to=ivan.calandra at uni-hamburg.de>>/*
>>    > wrote:
>>    >
>>    >
>>    >     From: Ivan Calandra <ivan.calandra at uni-hamburg.de
>>    </mc/compose?to=ivan.calandra at uni-hamburg.de>>
>>    >     Subject: Re: [R] Give me all operator
>>    >     To: r-help at r-project.org </mc/compose?to=r-help at r-project.org>
>>    >     Date: Wednesday, November 17, 2010, 1:44 PM
>>    >
>>    >     Hi Alex,
>>    >
>>    >     Is that what you're looking for:
>>    > > df <- data.frame(a=LETTERS[1:5], b=rnorm(5))
>>    > > df
>>    >        a          b
>>    >     1 A -0.2401323
>>    >     2 B -0.9414998
>>    >     3 C  0.4289836
>>    >     4 D  1.9802749
>>    >     5 E -0.6993612
>>    > > df[3,2]
>>    >     [1] 0.4289836
>>    > > df[3,]
>>    >        a         b
>>    >     3 C 0.4289836
>>    > > df[,2]
>>    >     [1] -0.2401323 -0.9414998  0.4289836  1.9802749 -0.6993612
>>    > > df[,2, drop=FALSE]
>>    >                 b
>>    >     1 -0.2401323
>>    >     2 -0.9414998
>>    >     3  0.4289836
>>    >     4  1.9802749
>>    >     5 -0.6993612
>>    >
>>    >     See ?"[" for help. In short, in R you use "[", not brackets
>>    as in
>>    >     matlab
>>    >     (from your example, I've never used it). You don't need the
>>    ":", you
>>    >     just don't write anything in R.
>>    >
>>    >     Ivan
>>    >
>>    >     Le 11/17/2010 14:34, Alaios a écrit :
>>    > > Hello is there in R any operator that give you all the data of a
>>    >     matrix
>>    > > for example in matlab
>>    > >
>>    > > x(2,3) returns the 2ndth row and 3rdth column
>>    > > x(2,:) returns all the columns of the 2nd row.
>>    > >
>>    > > In R now I would like to print all the
>>    > >
>>    > >   CRagent[[i]][2]
>>    > >
>>    > >
>>    > >   CRagent[[:]][2] doesnot work of course. Other option is to
>>    >     make a loop with an index i that spans from 1:last element of
>>    >     CRagent[[]] but this is not that optimal.
>>    > >
>>    > > I would like to thank you in aavance for your help
>>    > > Best Regards
>>    > > Alex
>>    > >
>>    > >
>>    > >
>>    > >
>>    > >     [[alternative HTML version deleted]]
>>    > >
>>    > >
>>    > >
>>    > > ______________________________________________
>>    > > R-help at r-project.org </mc/compose?to=R-help at r-project.org>
>>    </mc/compose?to=R-help at r-project.org
>>    </mc/compose?to=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.
>>    >
>>    >     --
>>    >     Ivan CALANDRA
>>    >     PhD Student
>>    >     University of Hamburg
>>    >     Biozentrum Grindel und Zoologisches Museum
>>    >     Abt. Säugetiere
>>    >     Martin-Luther-King-Platz 3
>>    >     D-20146 Hamburg, GERMANY
>>    >     +49(0)40 42838 6231
>>    > ivan.calandra at uni-hamburg.de
>>    </mc/compose?to=ivan.calandra at uni-hamburg.de>
>>    > </mc/compose?to=ivan.calandra at uni-hamburg.de
>>    </mc/compose?to=ivan.calandra at uni-hamburg.de>>
>>    >
>>    >     **********
>>    > http://www.for771.uni-bonn.de
>>    > http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>>    >
>>    >     ______________________________________________
>>    > R-help at r-project.org </mc/compose?to=R-help at r-project.org>
>>    </mc/compose?to=R-help at r-project.org
>>    </mc/compose?to=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.
>>    >
>>    >
>>
>>    --     Ivan CALANDRA
>>    PhD Student
>>    University of Hamburg
>>    Biozentrum Grindel und Zoologisches Museum
>>    Abt. Säugetiere
>>    Martin-Luther-King-Platz 3
>>    D-20146 Hamburg, GERMANY
>>    +49(0)40 42838 6231
>>    ivan.calandra at uni-hamburg.de
>>    </mc/compose?to=ivan.calandra at uni-hamburg.de>
>>
>>    **********
>>    http://www.for771.uni-bonn.de
>>    http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>>
>>    ______________________________________________
>>    R-help at r-project.org </mc/compose?to=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.
>>
>>
>
> --
> Ivan CALANDRA
> PhD Student
> University of Hamburg
> Biozentrum Grindel und Zoologisches Museum
> Abt. Säugetiere
> Martin-Luther-King-Platz 3
> D-20146 Hamburg, GERMANY
> +49(0)40 42838 6231
> ivan.calandra at uni-hamburg.de
>
> **********
> http://www.for771.uni-bonn.de
> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list