cd275
cd275

Reputation: 21

Return attributes within given radius of observation in R

Novice R programmer struggling to add a spatial component to large data set (>1500 obs)

Given a list of: observations, corresponding x/y points (not lat/long), and attributes; how can I write an algorithm to return the sum of variable "Z" for all observations within a given radius (10) of the observation in question. I need to do this for each observation.

Simplified data headers read as follows:

OBS      X        Y          Z**
A       56.55     -289.65  
B       52.59     -287.82   
C       58.34     -284.58     

Any assistance would be appreciated. Thanks.

Upvotes: 2

Views: 324

Answers (1)

Stephan Kolassa
Stephan Kolassa

Reputation: 8267

The key is to use mapply() to apply a function to multiple vectors at a time - here: the X and Y components of your data.frame. Which function? One that adds the Z variable for all observations in your data.frame that are at most radius away from the "anchor point" (which we run over all entries).

> set.seed(1)
> foo <- data.frame(X=runif(100),Y=runif(100),Z=runif(100))
> radius <- 0.5
> result <- mapply(function(X,Y,Z)
    sum(foo$Z[(foo$X-X)^2+(foo$Y-Y)^2<=radius^2]),foo$X,foo$Y)
> head(cbind(foo,result))
          X         Y         Z   result
1 0.2655087 0.6547239 0.2675082 24.99153
2 0.3721239 0.3531973 0.2186453 30.51512
3 0.5728534 0.2702601 0.5167968 28.15519
4 0.9082078 0.9926841 0.2689506 12.10058
5 0.2016819 0.6334933 0.1811683 22.50695
6 0.8983897 0.2132081 0.5185761 19.05273

Note that of course each point's Z itself is counted in its radius. If you don't want that, you'll need to subtract foo$Z.

Look at ?mapply.

Upvotes: 3

Related Questions