[R] Best way to store negative indexes

Liaw, Andy andy_liaw at merck.com
Wed Jul 28 03:53:23 CEST 2004


Can you give a simple example of what you are trying to do?

Would the following help?

> x <- sample(10)
> diffMat <- outer(x, x, "-")
> x
 [1]  1 10  5  8  9  6  3  2  4  7
> diffMat
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0   -9   -4   -7   -8   -5   -2   -1   -3    -6
 [2,]    9    0    5    2    1    4    7    8    6     3
 [3,]    4   -5    0   -3   -4   -1    2    3    1    -2
 [4,]    7   -2    3    0   -1    2    5    6    4     1
 [5,]    8   -1    4    1    0    3    6    7    5     2
 [6,]    5   -4    1   -2   -3    0    3    4    2    -1
 [7,]    2   -7   -2   -5   -6   -3    0    1   -1    -4
 [8,]    1   -8   -3   -6   -7   -4   -1    0   -2    -5
 [9,]    3   -6   -1   -4   -5   -2    1    2    0    -3
[10,]    6   -3    2   -1   -2    1    4    5    3     0
> diffTab <- table(diffMat[lower.tri(diffMat, diag=FALSE)])
> diffTab

-8 -7 -6 -5 -4 -3 -2 -1  1  2  3  4  5  6  7  8  9 
 1  2  3  3  3  4  5  4  5  3  3  3  2  1  1  1  1 
> diffTab <- table(abs(diffMat[lower.tri(diffMat, diag=FALSE)]))
> diffTab

1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 

Andy

> From: StephaneDemurget
> 
> Hi,
> 
> I'm trying to figure out how to properly construct a graph of 
> frequencies of a difference between 2 values, that is | i | - | j |.
> I'd like to know the best way to store the actual data 
> because of course 
> doing my_vector[i -j] will not work because the index could 
> be negative.
> 
> I know there's no hash table so what's the best solution, 
> simplicity-wise ? Use a list of pair of values {index, i - j} 
> ? Or can I 
> somehow use negative index, perhaps using 0array ? Any help would be 
> appreciated :)
> 
> ATM, I use as.character (i-j) to index my data, but I lose all the 
> processing power of R there after.
> 
> Sorry If it's written somewhere, but I read a lot of docs about it, 
> searched through the ML archives and didn't manage to find an answer.
> 
> Please also answer me back at my email address as I am not 
> subscribed to 
> the list.
> 
> Best regards,
> 
> --Stephane
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list