[R] Subsetting a list

Duncan Murdoch murdoch at stats.uwo.ca
Tue Oct 18 16:27:14 CEST 2005


On 10/18/2005 10:11 AM, Dennis Fisher wrote:
> Colleagues,
> 
> I have created a list in the following manner:
>      TEST    <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2"))
> 
> I now want to delete one element from the list, e.g., the third.  The  
> command
>      TEST[[3]]
> yields (as expected):
>      [1] "C1" "C2"
> 
> The command
>      TEST[[-3]]
> yields:
>      Error: attempt to select more than one element
> 
> How can I accomplish delete one or more elements from this list?

TEST[-3]

does what you want.  In general, single brackets say you want the answer 
to be a list, double brackets say you want to extract the element.

Duncan Murdoch




More information about the R-help mailing list