[R] moving distance between two sets of data

White, William Patrick white.232 at wright.edu
Sun Aug 19 21:04:48 CEST 2012


On the surface this seems pretty simple, but I flummoxed. I have two sets of numbers they bounce around zero, positive one and negative one. They have a relationship between them, where one diverges away from the other. I want create a second set of numbers that tracks that divergence. 

#Lets make some data like mine, kinda
Firstset <- runif(100, min = -1 , max =1)
Secondset <- runif(100, min = -1 , max =1)

#So something like:
Divergence <- abs (Firstset - Secondset)

#but this doesn't work because when Firstset is at .5 and Secondset is at  -.25 it returns .25 instead of .75

#another possibility is:

Divergence <- abs (Firstset) - abs (Secondset) 

#but when Firstset is at .5 and Secondset is at -.5 it returns 0 instead of 1

#It seems like there is a better way to do this. Any ideas?


More information about the R-help mailing list