I would like to know to which decile each number belongs compared to the
numbers in its column.
Say y[1,1] is the third decile among y[1:11,1] and y[2,1] is in the second
decile
I would like get a matrix that would return their ranks in decile, i.e.,
y[1,1] -> 3
y[2,1] -> 2
Your help is much appreciated!
Vincent -
I think
apply(y,2,function(x)
cut(x,quantile(x,(0:10)/10),label=FALSE,include.lowest=TRUE))
will give you what you want (although you didn't use set.seed so I
can't verify it against your example.)
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Thu, 6 May 2010, vincent.deluard wrote:
Hi R users,
I have a matrix of data similar to:
y=matrix(rnorm(55),ncol=5)
I would like to know to which decile each number belongs compared to the
numbers in its column.
Say y[1,1] is the third decile among y[1:11,1] and y[2,1] is in the second
decile
I would like get a matrix that would return their ranks in decile, i.e.,
y[1,1] -> 3
y[2,1] -> 2
Your help is much appreciated!
--
View this message in context: http://r.789695.n4.nabble.com/How-to-rank-matrix-data-by-deciles-tp2133496p2133496.html
Sent from the R help mailing list archive at Nabble.com.
Dear Phil,
You helped me with a request to rand matrix columns by deciles two weeks
ago.
This really unblocked me on this project but I found a little bug.
As in before, my data is in a matrix:
+ cut(x,quantile(x,(0:10)/10,
na.rm=TRUE),label=FALSE,include.lowest=TRUE))
Error in cut.default(x, quantile(x, (0:10)/10, na.rm = TRUE), label = FALSE,
:
'breaks' are not unique
My guess is that we now have 3 "zeros" in each column. For each decile, we
cannot have more than 2 elements (total of 17 numbers in each column) and I
believe R cannot determine where to put the third "zero". Do you have any
solution for this problem?
Many thanks,
--------------------------------------------
Vincent Deluard
Vincent.Deluard at TrimTabs.com
Global?Equity Strategist, CFA Charter Award Pending
TrimTabs Investment Research
40?Wall?Street, 28th Floor
New York, NY 10005
Phone: (+1) 646-512-5616
-----Original Message-----
From: Phil Spector [mailto:spector at stat.berkeley.edu]
Sent: Thursday, May 06, 2010 7:46 PM
To: vincent.deluard
Cc: r-help at r-project.org
Subject: Re: [R] How to rank matrix data by deciles?
Vincent -
I think
apply(y,2,function(x)
cut(x,quantile(x,(0:10)/10),label=FALSE,include.lowest=TRUE))
will give you what you want (although you didn't use set.seed so I
can't verify it against your example.)
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Thu, 6 May 2010, vincent.deluard wrote:
Hi R users,
I have a matrix of data similar to:
y=matrix(rnorm(55),ncol=5)
I would like to know to which decile each number belongs compared to the
numbers in its column.
Say y[1,1] is the third decile among y[1:11,1] and y[2,1] is in the second
decile
I would like get a matrix that would return their ranks in decile, i.e.,
y[1,1] -> 3
y[2,1] -> 2
Your help is much appreciated!
--
View this message in context:
Dear Phil,
You helped me with a request to rand matrix columns by deciles two weeks
ago.
This really un-blocked me on this project but I found a little bug.
As in before, my data is in a matrix:
+ cut(x,quantile(x,(0:10)/10,
na.rm=TRUE),label=FALSE,include.lowest=TRUE))
Error in cut.default(x, quantile(x, (0:10)/10, na.rm = TRUE), label = FALSE,
:
'breaks' are not unique
My guess is that we now have 3 "zeros" in each column. For each decile, we
cannot have more than 2 elements (total of 17 numbers in each column) and I
believe R cannot determine where to put the third "zero". Do you have any
solution for this problem?
Many thanks,
[1] 10 6 7 5 9 1 3 7 4 2 9 4 2 10 5 8 1
It gives an answer, but it may not make sense for all data.
- Phil
On Thu, 13 May 2010, vincent.deluard wrote:
Dear Phil,
You helped me with a request to rand matrix columns by deciles two weeks
ago.
This really un-blocked me on this project but I found a little bug.
As in before, my data is in a matrix:
+ cut(x,quantile(x,(0:10)/10,
na.rm=TRUE),label=FALSE,include.lowest=TRUE))
Error in cut.default(x, quantile(x, (0:10)/10, na.rm = TRUE), label = FALSE,
:
'breaks' are not unique
My guess is that we now have 3 "zeros" in each column. For each decile, we
cannot have more than 2 elements (total of 17 numbers in each column) and I
believe R cannot determine where to put the third "zero". Do you have any
solution for this problem?
Many thanks,
--
View this message in context: http://r.789695.n4.nabble.com/How-to-rank-matrix-data-by-deciles-tp2133496p2215945.html
Sent from the R help mailing list archive at Nabble.com.
[1] 10 6 7 5 9 1 3 7 4 2 9 4 2 10 5 8 1
It gives an answer, but it may not make sense for all data.
- Phil
The problem is that quantile() produces multiple values
for the breaks used in cut(). Phil's suggestion modifies
the data. It might be preferable to modify the breaks:
eps <- .Machine$double.eps #or use something like 1e-10
brks <- quantile(vec, (0:10)/10) + eps*(0:10)
cut(vec, brks, include.lowest=TRUE, labels=FALSE)
#[1] 10 6 7 5 9 1 3 7 4 2 9 4 1 10 5 8 1
-Peter Ehlers
On Thu, 13 May 2010, vincent.deluard wrote:
Dear Phil,
You helped me with a request to rand matrix columns by deciles two weeks
ago.
This really un-blocked me on this project but I found a little bug.
As in before, my data is in a matrix:
+ cut(x,quantile(x,(0:10)/10,
na.rm=TRUE),label=FALSE,include.lowest=TRUE))
Error in cut.default(x, quantile(x, (0:10)/10, na.rm = TRUE), label =
FALSE,
:
'breaks' are not unique
My guess is that we now have 3 "zeros" in each column. For each
decile, we
cannot have more than 2 elements (total of 17 numbers in each column)
and I
believe R cannot determine where to put the third "zero". Do you have any
solution for this problem?
Many thanks,
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-rank-matrix-data-by-deciles-tp2133496p2215945.html
Sent from the R help mailing list archive at Nabble.com.