[R] how to eliminate an element of a list

Michael Bedward michael.bedward at gmail.com
Fri Aug 13 13:29:09 CEST 2010


Hello André,

> I want to eliminate an element of a list:
>
> list <- seq(1,5,1)

That's not a list, it's a vector

> s <- sample(list,1)
>
> lets say s=3
> Now I want to remove 3 from the list: list2 = {1,2,4,5}

If all values are unique as in your example, this will work
x <- 1:5
s <- sample(x, 1)
x <- x[ x != s ]

The last step could also be...
x <- x[ -match(s, x) ]  # note minus sign

Lots of other was to do it too.

Note, I'm assuming you won't always have values equal to the element indices.

Michael



More information about the R-help mailing list