This can be tricky, because depending on what the missing object is, you can get either NULL, NA, or an error. Moreover is.na() behaves differently when evaluated on its own, or as the condition of an if() statement. Here is a function that may make life easier. The goal is NOT to have to pass extra arguments.
- I use try() and return FALSE if the evaluation returns an error. This applies to objects that are not found, incorrect syntax etc. - List elements that don't exist are NULL and return FALSE. - If any elements are NA, return FALSE. This handles out-of-bounds elements AND out-of-bounds slices on vectors. But it would also trip on valid vectors that contain an NA. I can't think of a good way to distinguish these two cases right now. The "best" way for this depends on the context. I think I am handling the most obvious special cases - though I do expect this can be improved. is.valid <- function(x, na.ignore = FALSE, null.ignore=FALSE) { # errors are always FALSE if (class(try(x, silent=TRUE)) == "try-error") return(FALSE) # NULL is FALSE except if ignored if (is.null(x)) { if (!null.ignore) return(FALSE) return(TRUE) } # If all elments are NA, return FALSE except if ignored; if (any(is.na(x))) { if (!na.ignore) return(FALSE) return(TRUE) } # Everything else is TRUE return(TRUE) } # Test cases is.valid(1) # TRUE: valid numeric constant is.valid(FALSE) # TRUE: valid boolean constant is.valid(nonSuch) # FALSE: object doesn't exist x <- 1:5; is.valid(x) # TRUE: existing variable is.valid(x[4]) # TRUE: vector element is.valid(x[8]) # FALSE: out of bounds: NA is.valid(x[5:6]) # FALSE: partially out of bounds: (5, NA) is.valid(x[8], na.ignore=TRUE) # TRUE x[3] <- NA is.valid(x) # FALSE: no element can be NA is.valid(x, na.ignore=TRUE) # TRUE m <- matrix(1:9,nrow=3, ncol=3) is.valid(m[2,2]) # TRUE is.valid(m[2,4]) # FALSE: subscript out of bounds is.valid(m[2,2,2]) # FALSE: incorrect n of dimensions l <- list(first=7, letters, NULL) is.valid(l[["first"]]) # TRUE: existing list elements is.valid(l[[4]]) # FALSE: list element does not exist is.valid(l[[2]][27]) # FALSE: out of bounds on existing element is.valid(l$first) # TRUE: is.valid(l$second) # FALSE: non-existent element: NULL is.valid(l$second, null.ignore=TRUE) # TRUE Cheers, B. On Dec 20, 2014, at 11:20 AM, Rui Barradas <ruipbarra...@sapo.pt> wrote: > Hello, > > Your list seems to have only 2 elements. You can check this with > > length(x) > > Or you can try > > lapply(x, is.null) > > Hope this helps, > > Rui Barradas > > Em 20-12-2014 15:58, Ragia Ibrahim escreveu: >> Hello, >> Kindly I have a list of lists as follow >> x >> [[1]] >> [1] 7 >> >> [[2]] >> [1] 3 4 5 >> >> as showen x[[3]] does not have a value and it has NULL, how can I check on >> this >> how to test if x[[3]] is empty. >> >> thanks in advance >> Ragia >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.