Say I have a list consisting of a range of integers from 1-10 with repetition and I want to remove all the 0s from that list, is there an easy way to go about doing that?
Something like na.omit but for my choice of elements?
Say I have a list consisting of a range of integers from 1-10 with repetition and I want to remove all the 0s from that list, is there an easy way to go about doing that?
Something like na.omit but for my choice of elements?
since you did not provide an example I create a list ls
ls <- list(rep(0:10, 10))
lsnew <- ls[[1]][ls[[1]] != 0]
It should be mentioned that lsnew
is a numeric vector, not a list!
This is a simple example of subsetting in R. For further information on subsetting different data structures refer to:
extending @loki's answer you can also exclude more than one element
ls <- list(rep(0:10, 10))
#excluding 0
lsnew <- ls[[1]][which(ls[[1]] != 0)]
#excluding 0 & 1
lsnew <- ls[[1]][! ls[[1]] %in% c(0,1)]