Hi, I want to eliminate an element of a list: list <- seq(1,5,1) s <- sample(list,1) lets say s=3 Now I want to remove 3 from the list: list2 = {1,2,4,5} Can someone give me a tip? Thanks, André [[alternative HTML version deleted]]
Try this: setdiff(list, s) On Thu, Aug 12, 2010 at 5:32 PM, André de Boer <rnieuws@gmail.com> wrote:> Hi, > > I want to eliminate an element of a list: > > list <- seq(1,5,1) > s <- sample(list,1) > > lets say s=3 > Now I want to remove 3 from the list: list2 = {1,2,4,5} > > Can someone give me a tip? > > Thanks, > André > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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
Try to do this> list <- seq(1,5,1) > list<-list[-3] > list[1] 1 2 4 5 list[-x] will remove the xth value in your list. Also you can do something like> list[-c(1:4)][1] 5 To remove values at indexes 1-4> list[-c(1,4)][1] 2 3 5 To remove values at indexes 1 and 4 -- View this message in context: http://r.789695.n4.nabble.com/how-to-eliminate-an-element-of-a-list-tp2323329p2324691.html Sent from the R help mailing list archive at Nabble.com.
> list<-seq(2,10,2) > list[1] 2 4 6 8 10> list[-which(2==list)][1] 4 6 8 10 using the which() will let you remove things from a list that have a specified value... I usually use the blah<- blah[-which(TRUE==is.na(blah)) ] which will remove all NA values in your list -- View this message in context: http://r.789695.n4.nabble.com/how-to-eliminate-an-element-of-a-list-tp2323329p2324696.html Sent from the R help mailing list archive at Nabble.com.
On Aug 13, 2010, at 4:06 PM, fishkbob wrote:> >> list<-seq(2,10,2) >> list > [1] 2 4 6 8 10 >> list[-which(2==list)] > [1] 4 6 8 10 > > using the which() will let you remove things from a list that have a > specified value... I usually use the > > blah<- blah[-which(TRUE==is.na(blah)) ] > > which will remove all NA values in your listAlthough which() is useful when used with "[" because it avoids the NA indexing problem, it is entirely superfluous here. blah<- blah[-is.na(blah) ] # will be more efficient>-- David Winsemius, MD West Hartford, CT