[R] newbie:looking for an efficient way to compute distance vector

Benilton Carvalho bcarvalh at jhsph.edu
Thu Jan 24 04:28:02 CET 2008


not so sure this helps much, but...

theDist <- function(x, step){
   ij <- combn(1:length(x), 2)
   mapply(function(i, j, step)
          (abs(d <- x[j]-x[i]) > step)*sign(d)*(j-i),
          ij[1,], ij[2,], step)
}

set.seed(123)
y <- rnorm(10^3)
system.time(tmp <- theDist(y, 1))

gives me:
    user  system elapsed
  20.287   0.038  20.347


this will be expensive anyways, because you don't say how big is big  
when you mention "as the length increases"... at some point, you want  
to trade the nested for()'s by vectorized versions, which may cost you  
memory to compute things like combn() or outer().

b

On Jan 23, 2008, at 4:39 PM, papagenic wrote:

>
> hello,
>
> I tried your suggestion ,but the first line :
> z <- outer(x, x, "-")
> seems to fail pretty quickly as the length of the x vector  
> increases.  This
> is probably because it has to create a matrix of dimension  
> dim(x)*dim(x). I
> am wondering if that can be quicker than a building a loop.
>
> if my vector x  holds a time series, I am trying to find for each  
> element i
> of x the nb of steps j so that the value  x[j]  differs from x[i] by  
> more
> than a predefined step value.
>
> Regards
>
>
> Benilton Carvalho wrote:
>>
>> i'm not so sure i understood, but you might want something in the
>> lines of:
>>
>> z <- outer(x, x, "-")
>> (abs(z)>step)*outer(1:length(x), 1:length(x))*z
>>
>> (not tested)
>>
>> b
>>
>>
>> On Jan 23, 2008, at 11:32 AM, papagenic wrote:
>>
>>>
>>> dear experts,
>>>
>>> I am new to R and am trying to compute a vector y from  a vector x
>>> where :
>>> y[i] = sign(x[j]-x[i])*(j-i)  with j the first index after i where
>>> abs(x[j]-x[i]) > to a given step
>>> y[i] is 0 if there is no such j
>>>
>>> I can write this in R as follows
>>> for(i in 1:length(x)) {
>>>  y[i]=0
>>>  for(j in i:length(x)) {
>>>     if (abs(x[j]-x[i]) > step) {
>>>        y[i]=sign(x[j]-x[i])*(j-i)
>>>        break;
>>>     }
>>>  }
>>> }
>>>
>>> but I wonder if there is a more efficient way to write this. I
>>> understand
>>> explicit looping can often be avoided in R using vector notation.
>>>
>>> Thanks for your help
>>> -- 
>>> View this message in context:
>>> http://www.nabble.com/newbie%3Alooking-for-an-efficient-way-to-compute-distance-vector-tp15045583p15045583.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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.
>>
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/newbie%3Alooking-for-an-efficient-way-to-compute-distance-vector-tp15045583p15053174.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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