[R] How to assign a rank to a range of values..
Alex Brown
alex at transitive.com
Wed Oct 11 09:33:46 CEST 2006
I don't know anything about SpatialGridDataFrames, but I can help you
with the generic data frame problems:
Let's generate some data:
dsize = 100
mydata = data.frame(
drainage = 100000*exp(exp(rnorm(1:dsize))),
x = round(runif(dsize,0,100)),
y = round(runif(dsize,0,100))
)
# calculate the rank
myrank = rank(mydata$drainage)
# attach to the data frame
mydata$rank = myrank
note that the rank generate above has only discrete values, and they
range from 1 to 100. It includes the possibility of ties, although
the double type I have used makes these unlikely.
I assume you have 180000 discrete measurements, and 6000 distinct
values, so you will have some ties. To generate ranks in the range 1
to 100 from the 1 to 180000 that will occur in your data, simply do
myrank = myrank * (100 / 180000) before attaching it to the data frame.
-Alex Brown
On 10 Oct 2006, at 20:46, Thomas P. Colson wrote:
>> From the following:
>
> basin.map <- readAsciiGrid("c:/temp/area.asc", colname="area")
>
> I have a SpatialGridDataFrame which has the x and y cordinate of a
> cell, and
> the drainage area of that cell. There are many cells with a low
> drainage
> area (in my case, 33000 with an area of 37.16) and one cell with
> the highest
> drainage area (again, in my case, a drainage area of of 800000).
>
> What I'd like to do, is to rank the drainage area cells based upon the
> number of times they occur, with a rank of 100 going to the cells with
> area=37.16, and 1 going to the cell(s) with area=800000). There are
> 6,000
> different drainage areas out of 180,000 cells in this grid, so the
> ranks
> would have values like 100, 99.01, 57.34, 20, 1.08, 1 and so forth.
>
>
> I have been struggeling with the split, length and rank commands in
> R, but
> can't seem to figure out how to "attach" a new column (or make a new
> dataset) that has a colums of ranks, or how to calculate the rank.
>
> Thanks for any help.
>
>
>
> Thomas Colson
> North Carolina State University
> Department of Forestry and Environmental Resources
> (919)624-6329
> (919)515 3434
> tpcolson at ncsu.edu
>
> Schedule: www4.ncsu.edu/~tpcolson
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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.
More information about the R-help
mailing list