user3910073
user3910073

Reputation: 531

From longitude and latitude to cell number

I have a data frame like this

 LonSHIP  LatSHIP LonINT LatINT
 -179.94  -38.05      1    128
 -179.93  -32.54      1    123
 -179.93  -32.19      1    122
 -179.93  -31.83      1    122
 -179.92  -33.97      1    124
 -179.92  -33.61      1    124

What I want is to associate at each (ordered) pair (LonINT,LatINT) a number to identify the grid cell.

E.g.

 LonSHIP  LatSHIP LonINT LatINT cell
 -179.94  -38.05      1    128   1
 -179.93  -32.54      1    123   2
 -179.93  -32.19      1    122   3
 -179.93  -31.83      1    122   3
 -179.92  -33.97      1    124   4
 -179.92  -33.61      1    124   4

Note that, for example

(1, 2) != (2,1), i.e. they must have two different cell number.

Can anyone help?

Upvotes: 0

Views: 69

Answers (1)

Prolix
Prolix

Reputation: 284

If you want ordered pairs then you need first to order by multiple columns:

dat<-dat[order(dat$LonINT,dat$LatINT,decreasing=F),]

Then you can use a simple formula to concatenate the two values. For example, if max(LatINT)<1000 you can do:

gridINT=dat$LonINT*1000+dat$LatINT

EDIT: Well, actually the first order is not needed with this formula because the ordering following the new gridINT identifier should give the same result.

Upvotes: 1

Related Questions