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]]
Hi, On Dec 20, 2014, at 10:58 AM, Ragia Ibrahim <ragia11 at hotmail.com> wrote:> 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. >In general you can us is.null() x <- list(7, 3:5, NULL, "A")> is.null(x[[3]])[1] TRUE but be aware that trying access an element by index that is greater than the length of the list will cause you issues.> is.null(x[[10]])Error in x[[10]] : subscript out of bounds You can make your own function to test for the existence of an element and if it is NULL. Note that the function isn't complete in the sense that it doesn't test if you provide an negative index, that x is not a list, etc. You can add all of those tests in. is_null <- function(x, index){ ( index[1] > length(x) ) || is.null(x[[index[1]]]) }> is_null(x, 1)[1] FALSE> is_null(x, 3)[1] TRUE> is_null(x, 10)[1] TRUE There a lot of info on index at> ?`[`Does that answer your question? Cheers, Ben> thanks in advance > Ragia > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org
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 at 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. >
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 <ruipbarradas at 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 at 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 at 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.
Your 'x' has length 2, so x[[3]] cannot be calculated ('subscript
out of
bounds' is what I get). You can check for this with length(x)<3.
In general, you want to be more precise: 'does not have a value',
'is
NULL', and 'is empty' are not synonymous. I'm not sure what
'does not have
a value' means to you. NULL is a value with a certain type, 'NULL',
and
length, 0. seq_len(0) is empty, but not NULL, since it has type
'integer'.
c(1,NA) contains a 'missing value'. Can you explain what what you are
trying to check for, giving some context?
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Sat, Dec 20, 2014 at 7:58 AM, Ragia Ibrahim <ragia11 at hotmail.com>
wrote:>
> 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 at 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.
>
[[alternative HTML version deleted]]
Dear group i have the following matrix 1 . . 1 . . 1 . . . . 2 . . . . . . 1 . . . 3 1 . . . 1 . . 1 . 1 4 . . . . . 1 . . . . 5 . . 1 . . . . . . 1 6 1 . . 1 . . . . 1 . 7 . 1 . . . . . 1 . . 8 . . 1 . . . 1 . . 1 9 . . . . . 1 . . . 1 10 . . 1 . 1 . . 1 1 . I want to sort it according to ones in each row ascending (where max number of ones first) to be as follow 3 1 . . . 1 . . 1 . 1 10 . . 1 . 1 . . 1 1 . 6 1 . . 1 . . . . 1 .8 . . 1 . . . 1 . . 11 . . 1 . . 1 . . . .5 . . 1 . . . . . . 17 . 1 . . . . . 1 . .9 . . . . . 1 . . . 12 . . . . . . 1 . . .4 . . . . . 1 . . . . how can I do this in R thanks in advance [[alternative HTML version deleted]]
The answer depends on what kind of matrix/data frame you have. That is why we encourage people to use dput() to create a copy of the sample data in their email. Some combination of order() function the rowSums() function will probably get you what you want. For example, dat[order(rowSums(dat=="1"), decreasing=TRUE),] or dat[order(rowSums(dat), decreasing=TRUE),] or dat[order(rowSums(dat, na.rm=TRUE), decreasing=TRUE),] Note that the order is not unique since there are ties in the number of 1s. ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ragia Ibrahim Sent: Monday, April 6, 2015 12:18 PM To: r-help at r-project.org Subject: [R] sort adjacency matrix Dear group i have the following matrix 1 . . 1 . . 1 . . . . 2 . . . . . . 1 . . . 3 1 . . . 1 . . 1 . 1 4 . . . . . 1 . . . . 5 . . 1 . . . . . . 1 6 1 . . 1 . . . . 1 . 7 . 1 . . . . . 1 . . 8 . . 1 . . . 1 . . 1 9 . . . . . 1 . . . 1 10 . . 1 . 1 . . 1 1 . I want to sort it according to ones in each row ascending (where max number of ones first) to be as follow 3 1 . . . 1 . . 1 . 1 10 . . 1 . 1 . . 1 1 . 6 1 . . 1 . . . . 1 .8 . . 1 . . . 1 . . 11 . . 1 . . 1 . . . .5 . . 1 . . . . . . 17 . 1 . . . . . 1 . .9 . . . . . 1 . . . 12 . . . . . . 1 . . .4 . . . . . 1 . . . . how can I do this in R thanks in advance [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Hello, You should have used ?dput to post your data example. Since you haven't, I've made up one. set.seed(4795) mat <- matrix(sample(0:1, 24, replace = TRUE), nrow = 6) mat inx <- order(rowSums(mat), decreasing = TRUE) mat[inx, ] Hope this helps, Rui Barradas Em 06-04-2015 18:18, Ragia Ibrahim escreveu:> Dear group > i have the following matrix > > 1 . . 1 . . 1 . . . . > 2 . . . . . . 1 . . . > 3 1 . . . 1 . . 1 . 1 > 4 . . . . . 1 . . . . > 5 . . 1 . . . . . . 1 > 6 1 . . 1 . . . . 1 . > 7 . 1 . . . . . 1 . . > 8 . . 1 . . . 1 . . 1 > 9 . . . . . 1 . . . 1 > 10 . . 1 . 1 . . 1 1 . > > I want to sort it according to ones in each row ascending (where max number of ones first) > > to be as follow > > 3 1 . . . 1 . . 1 . 1 > 10 . . 1 . 1 . . 1 1 . > 6 1 . . 1 . . . . 1 .8 . . 1 . . . 1 . . 11 . . 1 . . 1 . . . .5 . . 1 . . . . . . 17 . 1 . . . . . 1 . .9 . . . . . 1 . . . 12 . . . . . . 1 . . .4 . . . . . 1 . . . . > > how can I do this in R > thanks in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >