[R] vector manipulations -- differences
Dan D
ddalthorp at usgs.gov
Mon Sep 21 23:17:40 CEST 2015
I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x
as:
c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1])
x is increasing with x[1] = 0.
The following works but is not the greatest:
junk<-outer(x, x, '-')
junk[junk>0]
e.g.,
given
x<-c(0, 3, 7, 20)
junk<-outer(x, x, '-')
junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go
through
junk
# [,1] [,2] [,3] [,4]
#[1,] 0 -3 -7 -20
#[2,] 3 0 -4 -17
#[3,] 7 4 0 -13
#[4,] 20 17 13 0
Anyone have a better idea?
-Dan
--
View this message in context: http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list