[R] Selecting first 7 elements

David Winsemius dwinsemius at comcast.net
Sun May 30 16:31:30 CEST 2010


On May 30, 2010, at 9:44 AM, Kang Min wrote:

> Hmm x[8:14] didn't work. Yes Iverson's method worked, I wanted the
> whole list, with 7 elements in each vector.
>
> Now I want the whole list as well, but with the 8th to 14th element.

Oh, ... then in what sense did the lapply method not work? What code  
did you use? Did you get an error message? (It seems to be working  
fine for that purpose on my machine.)

-- 
David.

>
>
> On May 30, 7:47 pm, David Winsemius <dwinsem... at comcast.net> wrote:
>> On May 29, 2010, at 11:37 PM, Kang Min wrote:
>>
>>> What if I want to select the 8th to 14th element of the list? I  
>>> tried
>>> to use "[" again, but it doesn't work.
>>
>> x[8:14]
>>
>> (And I could not get Iverson's earlier method to work properly on a
>> long list. It just returned the whole list. Did it work properly for
>> you? )
>>
>> --
>> David.
>>
>>
>>
>>
>>
>>
>>
>>>>>> "[" is a function, and you want to use it on each element of the
>>>>>> list,
>>>>>> so...
>>
>>>>>> lapply(x, "[", c(1:7))
>>
>>>>> and the call to c() is of course not necessary, since ":" will
>>>>> generate a vector.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list