[R] column selection in list

baptiste auguie baptiste.auguie at googlemail.com
Fri Jan 22 18:50:41 CET 2010


?"[" should give you enough information. In short, "[" is an operator
to extract elements, you can think of it as a function with special
semantics. For a simple vector,

v = c("one", "two")

v[2] selects the second element of the vector, and is equivalent to,

`[`(v, 2)
# "two"

as you can see from,

call("[",v,2)
# c("one", "two")[2]


So, in lapply(), I specified "[" as the function to be applied to each
element of the list, and I passed an additional parameter indicating
which object should be selected ("y").

Best,

baptiste


2010/1/22 Ivan Calandra <ivan.calandra at uni-hamburg.de>:
> Thanks Baptiste, it does help.
>
> However, I don't really understand what "[" means. Could you please tell me
> more about it? I didn't find anything helpful on that in the help.
>
> Thanks in advance
> Ivan
>
>
>
> Le 1/22/2010 17:19, baptiste auguie a écrit :
>>
>> Hi,
>>
>> Try this,
>>
>> a = replicate(3, data.frame(x=1:10, y=rnorm(10)), simplify=FALSE)
>>
>> lapply(a, "[", "y")
>>
>> HTH,
>>
>> baptiste
>>
>> 2010/1/22 Ivan Calandra<ivan.calandra at uni-hamburg.de>:
>>
>>>
>>> Hi everybody!
>>>
>>> I have a (stupid) question but I cannot find a way to do it!
>>>
>>> I have a list like:
>>>
>>>>
>>>> SPECSHOR_tx_Asfc
>>>>
>>>
>>> $cotau
>>>   SPECSHOR Asfc.median
>>> 38    cotau    381.0247
>>> 39    cotau    154.6280
>>> 40    cotau    303.3219
>>> 41    cotau    351.2933
>>> 42    cotau    156.5327
>>> $eqgre
>>>    SPECSHOR Asfc.median
>>> 145    eqgre    219.5389
>>> 146    eqgre    162.5926
>>> 147    eqgre    146.3726
>>> 148    eqgre    127.6413
>>> 149    eqgre    274.2888
>>> $gicam
>>>    SPECSHOR Asfc.median
>>> 263    gicam    174.7445
>>> 264    gicam     83.4821
>>> 265    gicam    157.6005
>>> 266    gicam    153.7519
>>> 267    gicam    344.9775
>>>
>>> I would just like to remove the column "SPECSHOR" (or extract the other
>>> one)
>>> so that it looks like
>>> $cotau
>>>    Asfc.median
>>> 38        381.0247
>>> 39       154.6280
>>> 40        303.3219
>>> 41        351.2933
>>> 42        156.5327
>>> etc.
>>>
>>> How should I do it? I know how to select each element like
>>> SPECSHOR_tx_Asfc[[1]], but I don't know how to select a single column
>>> within
>>> an element.
>>>
>>> Could you please help me on that?
>>>
>>> Thanks
>>> Ivan
>>>
>>> ______________________________________________
>>> 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 Institut und Museum
> 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
>
>



More information about the R-help mailing list