list of lists, is this element empty

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

list of lists, is this element empty

Ragia
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list of lists, is this element empty

Ben Tupper-2
Hi,


On Dec 20, 2014, at 10:58 AM, Ragia Ibrahim <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list of lists, is this element empty

Rui Barradas
In reply to this post by Ragia
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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list of lists, is this element empty

Boris Steipe
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 <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list of lists, is this element empty

Bert Gunter
Boris et. al:

Indeed, corner cases are a bear, which is why it is incumbent on any
OP to precisely define what they mean by, say, "missing",
"null","empty", etc.

Here is an evil example to illustrate the sorts of nastiness that can occur:

> z <- list(a=NULL, b=list(), c=NA)

> with(z,{
+ c(identical(a,b),
+ identical(a,c),
+ identical(b,c)
+ )
+ })
[1] FALSE FALSE FALSE

## OK, none of these three are "the same" in the sense of identical().
But ...

> outer(z,z,identical)
Error in outer(z, z, identical) :
  dims [product 9] do not match the length of object [1]

## outer gets completely flummoxed, as it should!

> expand.grid(z,z)
  Var1 Var2
1 NULL NULL
2 NULL NULL
3   NA NULL
4 NULL NULL
5 NULL NULL
6   NA NULL
7 NULL   NA
8 NULL   NA
9   NA   NA

## and expand.grid gets confused, as it probably should.

:-)

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Sat, Dec 20, 2014 at 10:57 AM, Boris Steipe <[hidden email]> wrote:

> 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 <[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list of lists, is this element empty

Duncan Murdoch-2
In reply to this post by Boris Steipe
This may be out of context, but on the face of it, this claim is wrong:

On 20/12/2014, 1:57 PM, Boris Steipe wrote:
"Moreover is.na() behaves differently when evaluated on its own, or as
the condition of an if() statement."

The conditions in an if() statement are not evaluated in special
conditions at all.  The only way you'll get a different value is if the
argument to is.na() does tricky stuff like looking at the evaluation stack.

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list of lists, is this element empty

Boris Steipe
Thanks. This is what I was referring to:

x <- rep(NA, 3)
is.na(x)
[1] TRUE TRUE TRUE

if (is.na(x)) {print("True")}
[1] "True"
Warning message:
In if (is.na(x)) { :
  the condition has length > 1 and only the first element will be used

You are of course right - the warning is generated by if(), not by is.na() and the reason for the warning is that is.na() returns a vector if applied to a vector. I should have been more clear.

Cheers,
B.



On Dec 20, 2014, at 3:29 PM, Duncan Murdoch <[hidden email]> wrote:

> This may be out of context, but on the face of it, this claim is wrong:
>
> On 20/12/2014, 1:57 PM, Boris Steipe wrote:
> "Moreover is.na() behaves differently when evaluated on its own, or as
> the condition of an if() statement."
>
> The conditions in an if() statement are not evaluated in special
> conditions at all.  The only way you'll get a different value is if the
> argument to is.na() does tricky stuff like looking at the evaluation stack.
>
> Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list of lists, is this element empty

William Dunlap
In reply to this post by Ragia
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

sort adjacency matrix

Ragia
In reply to this post by Ragia
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sort adjacency matrix

David Carlson
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:[hidden email]] On Behalf Of Ragia Ibrahim
Sent: Monday, April 6, 2015 12:18 PM
To: [hidden email]
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]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sort adjacency matrix

Bert Gunter
Not quite, David.

If I understand the OP's query, he wants the ties to be broken by the
"lexicographic" order (with apologies if I have misused this term) of
the 1's within the rows. Makes things a bit more interesting.

Have at it!

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Mon, Apr 6, 2015 at 11:09 AM, David L Carlson <[hidden email]> wrote:

> 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:[hidden email]] On Behalf Of Ragia Ibrahim
> Sent: Monday, April 6, 2015 12:18 PM
> To: [hidden email]
> 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]]
>
> ______________________________________________
> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sort adjacency matrix

Rui Barradas
In reply to this post by Ragia
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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sort adjacency matrix_with data

Ragia
Hi again
the data represents a  directed graph  represented in a matrix

e.g
library(igraph)
g <- forest.fire.game(10, fw.prob=0.3 , bw.factor=0.32/0.3, directed = TRUE)

m=get.adjacency(g , attr=NULL)
print(m)

----------
many thanks and pardon me for multiple posts
Ragia

> Date: Mon, 6 Apr 2015 19:20:02 +0100
> From: [hidden email]
> To: [hidden email]; [hidden email]
> Subject: Re: [R] sort adjacency  matrix
>
> 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]]
> >
> > ______________________________________________
> > [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sort adjacency matrix

David Winsemius
In reply to this post by Bert Gunter

On Apr 6, 2015, at 11:15 AM, Bert Gunter wrote:

> Not quite, David.
>
> If I understand the OP's query, he wants the ties to be broken by the
> "lexicographic" order (with apologies if I have misused this term) of
> the 1's within the rows. Makes things a bit more interesting.

This should correct the problem:

> M <- as.matrix(m)
> M[ order(rowSums(M=="1"),
+           apply(M, 1, paste0, collapse=".") ,
+           decreasing=TRUE), ]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    1    1    0    0    0    0    0     0
 [2,]    0    0    0    1    1    0    1    0    0     0
 [3,]    1    0    0    0    0    0    0    0    0     0
 [4,]    1    0    0    0    0    0    0    0    0     0
 [5,]    1    0    0    0    0    0    0    0    0     0
 [6,]    0    1    0    0    0    0    0    0    0     0
 [7,]    0    1    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    1    0    0    0    0     0
 [9,]    0    0    0    0    1    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0

The original matrix was sparse and I get this error message when attempting to use 'order' in the i-argument to the `[` method for dgCMatrix:

Error in m[order(rowSums(m == "1"), apply(m, 1, paste0, collapse = "."),  :
  error in evaluating the argument 'i' in selecting a method for function '[': Error: not-yet-implemented method for ==(<dgCMatrix>, <character>).
 ->>  Ask the package authors to implement the missing feature.


HTH;
--
David.


> Have at it!
>
> Cheers,
> Bert
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
> (650) 467-7374
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> Clifford Stoll
>
>
>
>
> On Mon, Apr 6, 2015 at 11:09 AM, David L Carlson <[hidden email]> wrote:
>> 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:[hidden email]] On Behalf Of Ragia Ibrahim
>> Sent: Monday, April 6, 2015 12:18 PM
>> To: [hidden email]
>> 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
>>
>


David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

get the first row ?

Ragia
In reply to this post by Ragia
Dear group
kindly

I have a logical data type
ISINFCluster:
    1     2     3     4     5     6     7     8     9    10    11    12    13    14
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
   15    16    17    18    19    20    21    22    23    24    25    26    27    28
FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
   29    30    31    32    33    34    35    36    37    38    39    40    41    42
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
   43    44    45    46    47    48    49    50
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

and I want to get just number of records from those that has the  value true, thus

 firstclass<-     all[ISINCluster]  
where  all is numeric object
1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 1  1  2  1  3  1  1  2  5  4  1  6  8  1  2  1  3  3  2 13  1  4  2  4  7  1 14  1  1
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 1  1  2  2  1  1  2  2  2  9  3  1  1  3  6  1  4  1  3  9  2

the firstclass containes the following
firstclass
20 27
13 14

And I could not get the first row..
head(firstclass)
20 27
13 14


how can I got it?
thanks in advance
Ragia



     
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: get the first row ?

Jim Lemon-4
Hi Ragia,
This is a bit cryptic. "ISINFCluster" looks like a 50 element logical
vector with two TRUE values.
"all" looks like a 50 element numeric vector of counts for each value.
"firstclass" contains the 20th and 27th elements of "all", selected
with "ISINFCluster".
As "firstclass" is a two element named vector, there is only one "row"
and that includes the entire object.

Jim


On Sun, May 10, 2015 at 7:26 PM, Ragia Ibrahim <[hidden email]> wrote:

> Dear group
> kindly
>
> I have a logical data type
> ISINFCluster:
>     1     2     3     4     5     6     7     8     9    10    11    12    13    14
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>    15    16    17    18    19    20    21    22    23    24    25    26    27    28
> FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
>    29    30    31    32    33    34    35    36    37    38    39    40    41    42
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>    43    44    45    46    47    48    49    50
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> and I want to get just number of records from those that has the  value true, thus
>
>  firstclass<-     all[ISINCluster]
> where  all is numeric object
> 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
>  1  1  2  1  3  1  1  2  5  4  1  6  8  1  2  1  3  3  2 13  1  4  2  4  7  1 14  1  1
> 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
>  1  1  2  2  1  1  2  2  2  9  3  1  1  3  6  1  4  1  3  9  2
>
> the firstclass containes the following
> firstclass
> 20 27
> 13 14
>
> And I could not get the first row..
> head(firstclass)
> 20 27
> 13 14
>
>
> how can I got it?
> thanks in advance
> Ragia
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

get sub elements

Ragia
In reply to this post by Ragia
Dear group
I have this numeric object
allrecords
1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 1  1  2  1  3  1  1  2  5  4  1  6  8  1  2  1  3  3  2 13  1  4  2  4  7  1 14  1  1
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 1  1  2  2  1  1  2  2  2  9  3  1  1  3  6  1  4  1  3  9  2


how can I extract only records between 9 and 5 values only  and put the record number n a vector

it should be vector
thanks in advance
Ragia



         
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: get the first row ?

J Robertson-Burns
In reply to this post by Ragia
It looks to me like you need to understand
subscripting in R.  One place (among many)
to learn subscripting is:

http://www.burns-stat.com/documents/tutorials/impatient-r/

Pat

On 10/05/2015 10:26, Ragia Ibrahim wrote:

> Dear group
> kindly
>
> I have a logical data type
> ISINFCluster:
>      1     2     3     4     5     6     7     8     9    10    11    12    13    14
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>     15    16    17    18    19    20    21    22    23    24    25    26    27    28
> FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
>     29    30    31    32    33    34    35    36    37    38    39    40    41    42
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>     43    44    45    46    47    48    49    50
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> and I want to get just number of records from those that has the  value true, thus
>
>   firstclass<-     all[ISINCluster]
> where  all is numeric object
> 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
>   1  1  2  1  3  1  1  2  5  4  1  6  8  1  2  1  3  3  2 13  1  4  2  4  7  1 14  1  1
> 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
>   1  1  2  2  1  1  2  2  2  9  3  1  1  3  6  1  4  1  3  9  2
>
> the firstclass containes the following
> firstclass
> 20 27
> 13 14
>
> And I could not get the first row..
> head(firstclass)
> 20 27
> 13 14
>
>
> how can I got it?
> thanks in advance
> Ragia
>
>
>
>    
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: get sub elements

Rui Barradas
In reply to this post by Ragia
Hello,

You should learn about indexing in R. Read the pdf R-intro.pdf that
comes with your installation of R.

allrecords[5 <= allrecords & allrecords <= 9]

should do it.

Hope this helps,

Rui Barradas

Em 10-05-2015 11:00, Ragia Ibrahim escreveu:

> Dear group
> I have this numeric object
> allrecords
> 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
>   1  1  2  1  3  1  1  2  5  4  1  6  8  1  2  1  3  3  2 13  1  4  2  4  7  1 14  1  1
> 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
>   1  1  2  2  1  1  2  2  2  9  3  1  1  3  6  1  4  1  3  9  2
>
>
> how can I extract only records between 9 and 5 values only  and put the record number n a vector
>
> it should be vector
> thanks in advance
> Ragia
>
>
>
>        
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: get sub elements

Boris Steipe
I think the "record number" i.e. the indices of the elements were asked for. That would be:

which(5 <= allrecords  & allrecords <= 9)


Cheers,
B.








On May 10, 2015, at 7:33 AM, Rui Barradas <[hidden email]> wrote:

> Hello,
>
> You should learn about indexing in R. Read the pdf R-intro.pdf that comes with your installation of R.
>
> allrecords[5 <= allrecords & allrecords <= 9]
>
> should do it.
>
> Hope this helps,
>
> Rui Barradas
>
> Em 10-05-2015 11:00, Ragia Ibrahim escreveu:
>> Dear group
>> I have this numeric object
>> allrecords
>> 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
>>  1  1  2  1  3  1  1  2  5  4  1  6  8  1  2  1  3  3  2 13  1  4  2  4  7  1 14  1  1
>> 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
>>  1  1  2  2  1  1  2  2  2  9  3  1  1  3  6  1  4  1  3  9  2
>>
>>
>> how can I extract only records between 9 and 5 values only  and put the record number n a vector
>>
>> it should be vector
>> thanks in advance
>> Ragia
>>
>>
>>
>>        
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
12