Hi, I have a matrix not unlike this:
foo <- matrix(,5,5)
foo[5,1] <- 1
foo[1:3,2] <- 1
foo[3:4,3] <- 1
foo[4:5,4] <- 1
foo[2:4,5] <- 1
foo
[,1] [,2] [,3] [,4] [,5]
[1,] NA 1 NA NA NA
[2,] NA 1 NA NA 1
[3,] NA 1 1 NA 1
[4,] NA NA 1 1 1
[5,] 1 NA NA 1 NA
I want to get a vector that is the column numbers as sorted by the first
non-NA value.
Like this:
2,5,3,4,1
I have been able to do this by adding an index and looping the matrix by
column. Can anybody think of a cleverer way to do this?
Thanks, as always, in advance.
Andy
I assume you mean "a vector that is the column numbers as sorted by the *index* of the first non-NA value" I think this will do what you want: > order(apply(foo, 2, function(col) which(!is.na(col))[1])) [1] 2 5 3 4 1 > Fortuitously, this also gives correct results when a column contains all NA values (because [1] of an empty vector returns NA, and the default for order() is to put NA values last). hope this helps, Tony Plate At Monday 10:18 PM 5/12/2003 -0600, Andy Bunn wrote:>Hi, I have a matrix not unlike this: > >foo <- matrix(,5,5) >foo[5,1] <- 1 >foo[1:3,2] <- 1 >foo[3:4,3] <- 1 >foo[4:5,4] <- 1 >foo[2:4,5] <- 1 >foo > [,1] [,2] [,3] [,4] [,5] >[1,] NA 1 NA NA NA >[2,] NA 1 NA NA 1 >[3,] NA 1 1 NA 1 >[4,] NA NA 1 1 1 >[5,] 1 NA NA 1 NA > >I want to get a vector that is the column numbers as sorted by the first >non-NA value. >Like this: >2,5,3,4,1 > >I have been able to do this by adding an index and looping the matrix by >column. Can anybody think of a cleverer way to do this? > >Thanks, as always, in advance. > >Andy > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
You can use unique since it keeps the ordering intact (ie no sorting).
unique( unlist( apply(foo, 1, function(x) which(!is.na(x)) ) ) )
[1] 2 5 3 4 1
-----Original Message-----
From: Andy Bunn [mailto:abunn at montana.edu]
Sent: Tuesday, May 13, 2003 12:18 PM
To: 'R-Help'
Subject: [R] Sorting a matrix in an odd way
Hi, I have a matrix not unlike this:
foo <- matrix(,5,5)
foo[5,1] <- 1
foo[1:3,2] <- 1
foo[3:4,3] <- 1
foo[4:5,4] <- 1
foo[2:4,5] <- 1
foo
[,1] [,2] [,3] [,4] [,5]
[1,] NA 1 NA NA NA
[2,] NA 1 NA NA 1
[3,] NA 1 1 NA 1
[4,] NA NA 1 1 1
[5,] 1 NA NA 1 NA
I want to get a vector that is the column numbers as sorted by the first
non-NA value. Like this:
2,5,3,4,1
I have been able to do this by adding an index and looping the matrix by
column. Can anybody think of a cleverer way to do this?
Thanks, as always, in advance.
Andy
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Try this, and tell me if it works.
foo <- matrix(,5,5)
foo[5,1] <- 1
foo[1:3,2] <- 1
foo[3:4,3] <- 1
foo[4:5,4] <- 1
foo[2:4,5] <- 1
foo
[,1] [,2] [,3] [,4] [,5]
[1,] NA 1 NA NA NA
[2,] NA 1 NA NA 1
[3,] NA 1 1 NA 1
[4,] NA NA 1 1 1
[5,] 1 NA NA 1 NA
I don't now if the rest non-NA values are only one, I suppose...
foo[is.na(foo)]<-0
biny<-function(x,l) sum(x*2^((l-1):0))
order(apply(foo,2,biny,nrow(foo)),decreasing=TRUE)
Hope, the idea helps.
Best Regards
Kenneth
Andy Bunn wrote:
>Hi, I have a matrix not unlike this:
>
>foo <- matrix(,5,5)
>foo[5,1] <- 1
>foo[1:3,2] <- 1
>foo[3:4,3] <- 1
>foo[4:5,4] <- 1
>foo[2:4,5] <- 1
>foo
> [,1] [,2] [,3] [,4] [,5]
>[1,] NA 1 NA NA NA
>[2,] NA 1 NA NA 1
>[3,] NA 1 1 NA 1
>[4,] NA NA 1 1 1
>[5,] 1 NA NA 1 NA
>
>I want to get a vector that is the column numbers as sorted by the first
>non-NA value.
>Like this:
>2,5,3,4,1
>
>I have been able to do this by adding an index and looping the matrix by
>column. Can anybody think of a cleverer way to do this?
>
>Thanks, as always, in advance.
>
>Andy
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
>
>
--
Kenneth Roy Cabrera Torres
Celular +57 (315) 405 9339