[R] indexing question

Kaskelma, Heikki kashei at sip-oy.com
Tue Nov 14 17:42:04 CET 2006


idx = c(31, 36, 41, 61, 66, 71, 91, 96, 101, 121, 126, 131)
temp = 1:150
res = temp[idx]
dim(res) = c(3, length(res) %/% 3)
res = t(rbind(res, res[2, ] - res[1, ], res[3, ] - res[2, ]))
res


Heikki Kaskelma

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Leeds, Mark (IED)
Sent: Monday, November 13, 2006 10:06 PM
To: r-help at stat.math.ethz.ch
Subject: [R] indexing question

I have the following set of indices, call it idx, that correspond to the
indices of a vector say temp.

 [1]   31   36   41   61   66   71   91   96  101  121  126  131  151
156  161  181  186  191  211  216  221  241  246  251  271  276  281
301  306  311  331  336  341  361  366
 [36]  371  391  396  401  421  426  431  451  456  461  481  486  491
511  516  521  541  546  551  571  576  581  601  606  611  631  636
641  661  666  671  691  696  701  721
 [71]  726  731  751  756  761  781  786  791  811  816  821  841  846
851  871  876  881  901  906  911  931  936  941  961  966  971  991
996 1001 1021 1026 1031 1051 1056 1061
[106] 1081 1086 1091 1111 1116 1121 1141 1146 1151 1171 1176 1181 1201
1206 1211 1231 1236 1241 1261 1266 1271 1291 1296 1301 1321 1326 1331
1351 1356 1361 1381 1386 1391 1411 1416
[141] 1421


I want to calculate temp[36] - temp[31] and temp[41] - temp[36]

Similarly, temp[66] - temp[61] and temp[71] - temp[66]
.
.
.
.
Similarly temp[1416]-temp[1411]
          temp[1421] - temp[1416]


I'm doing this because the above subractions represent pairs of returns
and the correlations between them wil be calculated eventually.

In other words, eventually I will have    

X_36_31  ( i.e : temp[36] - temp[31] )
X_66-61
X_96-91
.
.
.
.
.
.
.
X_1411-1416 

as one vector and

Y_41-36
Y_71-66
Y_101-96
.
.
.
.
.
Y_1416_1421

as another vector.

and will calculate the correlation between the two vectors in order to
get one number.


The point is I am  really only using the indices 31, 61, 91 etc as
anchor's so a regular diff(temp[idx]) won't work because it will diff
all 
the elements that are next to each other ? This is a weird problem. I'm
still thinking about it. I'm hoping to figure it out before someone
sends me something but I won't mind so much if I get an external
solution first. I have no pride.
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to
buy/se...{{dropped}}

______________________________________________
R-help at stat.math.ethz.ch 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