for loop - R: Vectorizing a condition -


i have dataset on million rows , 67 columns. create new column records scores according code below.

i stuck @ condition need take care of in:

df$change[df[,63] == "m"] <- df$change[df[,63] == "m"] - pos2 

when df$change[df[,63] == "m"] - pos2 = 0 need see if happend on bidside or askside. way can determine seeing if position of df[,64] even(askside) or odd(bidside). there's caveat, can't use value pos2 , have calculated, because of function called mywhich(i can provide code if needed) used in code. determine even/odd have recalculate position of df[,64]. once know or odd, df$change should either -1 or 1 depending on whether df[,66] > df[,64] or <.

now, i've tried subsetting don't see how can work because have recalculate positions. tried not using mywhich part cant seem head around make work.

any pointers/suggestions? else should try? should write separate function handles this? write version of function? little lost

this have far:

 > df$change <- apply(df[, 1:62] == df[,64], 1, mywhich)     > df$change[df[,63] == "c"] <- apply(df[which(df[,63] == "c") - 1, 1:62] == df[df[,63] == "c",64], 1, mywhich)*(-1)      > pos2 <- apply(df[which(df[,63] == "m") - 1, 1:62] == df[df[,63] == "m",66], 1, mywhich)      > df$change[df[,63] == "m"] <- df$change[df[,63] == "m"] - pos2 

this output:

> head(df, 20)                   datetime  seq   bp1 bq1 bo1   ap1 aq1 ao1   bp2 bq2 bo2   ap2 aq2 ao2   bp3 bq3 bo3   ap3 aq3 ao3   bp4 bq4 bo4   ap4 aq4 ao4   bp5 bq5 bo5   ap5 aq5 ao5 bp6 bq6 bo6   ap6 aq6 ao6 bp7 bq7 bo7   ap7 aq7 ao7 bp8 bq8 bo8 ap8 aq8 ao8 bp9 bq9 bo9 ap9 1  2015-11-30 09:15:00.368   92 80830   1   1     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 2  2015-11-30 09:15:00.368  108 80830   1   1 83435   1   1     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 3  2015-11-30 09:15:00.375  406 81100   1   1 83435   1   1 80830   1   1     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 4  2015-11-30 09:15:00.375  479 81100   1   1 82165   1   1 80830   1   1 83435   1   1     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0     0   0   0   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 5  2015-11-30 09:15:00.377  643 81100   1   1 82165   1   1 80830   1   1 83200   1   1     0   0   0 83435   1   1     0   0   0     0   0   0     0   0   0     0   0   0   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 6  2015-11-30 09:15:00.378  722 81100   1   1 82165   1   1 80830   1   1 82650   1   1     0   0   0 83200   1   1     0   0   0 83435   1   1     0   0   0     0   0   0   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 7  2015-11-30 09:15:00.380  811 81100   1   1 82165   1   1 80830   1   1 82650   1   1     0   0   0 83200   1   1     0   0   0 83430   1   1     0   0   0 83435   1   1   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 8  2015-11-30 09:15:00.380  822 81100   1   1 82165   1   1 80835   1   1 82650   1   1 80830   1   1 83200   1   1     0   0   0 83430   1   1     0   0   0 83435   1   1   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 9  2015-11-30 09:15:00.380  828 81100   1   1 82345   1   1 80835   1   1 82650   1   1 80830   1   1 83200   1   1     0   0   0 83430   1   1     0   0   0 83435   1   1   0   0   0     0   0   0   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 10 2015-11-30 09:15:00.383 1046 81100   1   1 82345   1   1 80835   1   1 82650   1   1 80830   1   1 83200   1   1     0   0   0 83430   1   1     0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 11 2015-11-30 09:15:00.384 1103 81100   1   1 82165   1   1 80835   1   1 82650   1   1 80830   1   1 83200   1   1     0   0   0 83430   1   1     0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 12 2015-11-30 09:15:00.384 1171 81100   1   1 82345   1   1 80835   1   1 82650   1   1 80830   1   1 83200   1   1     0   0   0 83430   1   1     0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0     0   0   0   0   0   0   0   0   0   0   0   0   0 13 2015-11-30 09:15:00.384 1186 81100   1   1 82345   1   1 80835   1   1 82650   1   1 80830   1   1 82900   1   1     0   0   0 83200   1   1     0   0   0 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0 14 2015-11-30 09:15:00.384 1196 81100   1   1 82165   1   1 80835   1   1 82650   1   1 80830   1   1 82900   1   1     0   0   0 83200   1   1     0   0   0 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0 15 2015-11-30 09:15:00.385 1238 81100   1   1 82340   1   1 80835   1   1 82650   1   1 80830   1   1 82900   1   1     0   0   0 83200   1   1     0   0   0 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0 16 2015-11-30 09:15:00.385 1249 81100   1   1 82340   1   1 80835   1   1 82650   1   1 80830   1   1 82900   1   1     0   0   0 83200   2   1     0   0   0 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0 17 2015-11-30 09:15:00.385 1254 81200   1   1 82340   1   1 81100   1   1 82650   1   1 80835   1   1 82900   1   1 80830   1   1 83200   2   1     0   0   0 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0 18 2015-11-30 09:15:00.387 1273 81200   1   1 82340   1   1 81100   1   1 82650   1   1 80835   1   1 82900   1   1 80830   1   1 83200   2   1 80035   1   1 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0 19 2015-11-30 09:15:00.388 1333 81200   1   1 82165   1   1 81100   1   1 82650   1   1 80835   1   1 82900   1   1 80830   1   1 83200   2   1 80035   1   1 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0 20 2015-11-30 09:15:00.388 1343 81200   1   1 82340   1   1 81100   1   1 82650   1   1 80835   1   1 82900   1   1 80830   1   1 83200   2   1 80035   1   1 83430   1   1   0   0   0 83435   1   1   0   0   0 83500   1   1   0   0   0   0   0   0   0   0   0   0    aq9 ao9 bp10 bq10 bo10 ap10 aq10 ao10 c price qty oldprice oldqty change 1    0   0    0    0    0    0    0    0 n 80830   1       na     na      5 2    0   0    0    0    0    0    0    0 n 83435   1       na     na      5 3    0   0    0    0    0    0    0    0 n 81100   1       na     na      5 4    0   0    0    0    0    0    0    0 n 82165   1       na     na      5 5    0   0    0    0    0    0    0    0 n 83200   1       na     na      4 6    0   0    0    0    0    0    0    0 n 82650   1       na     na      4 7    0   0    0    0    0    0    0    0 n 83430   1       na     na      2 8    0   0    0    0    0    0    0    0 n 80835   1       na     na      4 9    0   0    0    0    0    0    0    0 m 82345   1    82165      1      0 10   0   0    0    0    0    0    0    0 n 83500   1       na     na      0 11   0   0    0    0    0    0    0    0 m 82165   1    82345      1      0 12   0   0    0    0    0    0    0    0 m 82345   1    82165      1      0 13   0   0    0    0    0    0    0    0 n 82900   1       na     na      3 14   0   0    0    0    0    0    0    0 m 82165   1    82345      1      0 15   0   0    0    0    0    0    0    0 m 82340   1    82165      1      0 16   0   0    0    0    0    0    0    0 n 83200   1       na     na      2 17   0   0    0    0    0    0    0    0 n 81200   1       na     na      5 18   0   0    0    0    0    0    0    0 n 80035   1       na     na      1 19   0   0    0    0    0    0    0    0 m 82165   1    82340      1      0 20   0   0    0    0    0    0    0    0 m 82340   1    82165      1      0  > dput(df[1:20,]) structure(list(datetime = structure(c(1448855100.369, 1448855100.369,  1448855100.375, 1448855100.376, 1448855100.378, 1448855100.379,  1448855100.38, 1448855100.38, 1448855100.38, 1448855100.383,  1448855100.384, 1448855100.385, 1448855100.385, 1448855100.385,  1448855100.386, 1448855100.386, 1448855100.386, 1448855100.387,  1448855100.389, 1448855100.389), class = c("posixct", "posixt" ), tzone = ""), seq = c(92l, 108l, 406l, 479l, 643l, 722l, 811l,  822l, 828l, 1046l, 1103l, 1171l, 1186l, 1196l, 1238l, 1249l,  1254l, 1273l, 1333l, 1343l), bp1 = c(80830l, 80830l, 81100l,  81100l, 81100l, 81100l, 81100l, 81100l, 81100l, 81100l, 81100l,  81100l, 81100l, 81100l, 81100l, 81100l, 81200l, 81200l, 81200l,  81200l), bq1 = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), bo1 = c(1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l ), ap1 = c(0l, 83435l, 83435l, 82165l, 82165l, 82165l, 82165l,  82165l, 82345l, 82345l, 82165l, 82345l, 82345l, 82165l, 82340l,  82340l, 82340l, 82340l, 82165l, 82340l), aq1 = c(0l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l), ao1 = c(0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), bp2 = c(0l, 0l, 80830l, 80830l,  80830l, 80830l, 80830l, 80835l, 80835l, 80835l, 80835l, 80835l,  80835l, 80835l, 80835l, 80835l, 81100l, 81100l, 81100l, 81100l ), bq2 = c(0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l), bo2 = c(0l, 0l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), ap2 = c(0l,  0l, 0l, 83435l, 83200l, 82650l, 82650l, 82650l, 82650l, 82650l,  82650l, 82650l, 82650l, 82650l, 82650l, 82650l, 82650l, 82650l,  82650l, 82650l), aq2 = c(0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), ao2 = c(0l, 0l,  0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l), bp3 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 80830l, 80830l,  80830l, 80830l, 80830l, 80830l, 80830l, 80830l, 80830l, 80835l,  80835l, 80835l, 80835l), bq3 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), bo3 = c(0l,  0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l), ap3 = c(0l, 0l, 0l, 0l, 83435l, 83200l, 83200l,  83200l, 83200l, 83200l, 83200l, 83200l, 82900l, 82900l, 82900l,  82900l, 82900l, 82900l, 82900l, 82900l), aq3 = c(0l, 0l, 0l,  0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l), ao3 = c(0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), bp4 = c(0l, 0l, 0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 80830l, 80830l, 80830l,  80830l), bq4 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l), bo4 = c(0l, 0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l ), ap4 = c(0l, 0l, 0l, 0l, 0l, 83435l, 83430l, 83430l, 83430l,  83430l, 83430l, 83430l, 83200l, 83200l, 83200l, 83200l, 83200l,  83200l, 83200l, 83200l), aq4 = c(0l, 0l, 0l, 0l, 0l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l), ao4 = c(0l,  0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l), bp5 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 80035l, 80035l, 80035l), bq5 = c(0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,  1l, 1l, 1l), bo5 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l), ap5 = c(0l, 0l, 0l,  0l, 0l, 0l, 83435l, 83435l, 83435l, 83435l, 83435l, 83435l, 83430l,  83430l, 83430l, 83430l, 83430l, 83430l, 83430l, 83430l), aq5 = c(0l,  0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l), ao5 = c(0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), bp6 = c(0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,  0l), bq6 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bo6 = c(0l, 0l, 0l, 0l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l),      ap6 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 83500l, 83500l,      83500l, 83435l, 83435l, 83435l, 83435l, 83435l, 83435l, 83435l,      83435l), aq6 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l,      1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), ao6 = c(0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,      1l, 1l, 1l), bp7 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bq7 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), bo7 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), ap7 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 83500l, 83500l,      83500l, 83500l, 83500l, 83500l, 83500l, 83500l), aq7 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l,      1l, 1l, 1l, 1l), ao7 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), bp8 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), bq8 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bo8 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), ap8 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), aq8 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), ao8 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bp9 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), bq9 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bo9 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), ap9 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), aq9 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), ao9 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bp10 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), bq10 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bo10 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), ap10 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), aq10 = c(0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l), ao10 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,      0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), c = structure(c(4l,      4l, 4l, 4l, 4l, 4l, 4l, 4l, 3l, 4l, 3l, 3l, 4l, 3l, 3l, 4l,      4l, 4l, 3l, 3l), .label = c("", "c", "m", "n"), class = "factor"),      price = c(80830l, 83435l, 81100l, 82165l, 83200l, 82650l,      83430l, 80835l, 82345l, 83500l, 82165l, 82345l, 82900l, 82165l,      82340l, 83200l, 81200l, 80035l, 82165l, 82340l), qty = c(1l,      1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,      1l, 1l, 1l, 1l), oldprice = c(na, na, na, na, na, na, na,      na, 82165l, na, 82345l, 82165l, na, 82345l, 82165l, na, na,      na, 82340l, 82165l), oldqty = c(na, na, na, na, na, na, na,      na, 1l, na, 1l, 1l, na, 1l, 1l, na, na, na, 1l, 1l), change = c(5,      5, 5, 5, 4, 4, 2, 4, 0, 0, 0, 0, 3, 0, 0, 2, 5, 1, 0, 0)), .names = c("datetime",  "seq", "bp1", "bq1", "bo1", "ap1", "aq1", "ao1", "bp2", "bq2",  "bo2", "ap2", "aq2", "ao2", "bp3", "bq3", "bo3", "ap3", "aq3",  "ao3", "bp4", "bq4", "bo4", "ap4", "aq4", "ao4", "bp5", "bq5",  "bo5", "ap5", "aq5", "ao5", "bp6", "bq6", "bo6", "ap6", "aq6",  "ao6", "bp7", "bq7", "bo7", "ap7", "aq7", "ao7", "bp8", "bq8",  "bo8", "ap8", "aq8", "ao8", "bp9", "bq9", "bo9", "ap9", "aq9",  "ao9", "bp10", "bq10", "bo10", "ap10", "aq10", "ao10", "c", "price",  "qty", "oldprice", "oldqty", "change"), row.names = c(na, 20l ), class = "data.frame") 

scroll down find dput(df)

i came bottom of this, figured create many new column wanted in order calculate had that's did. think best way open suggestions if there other ways or faster.

here code:

df<- read.csv(file = file,header = false,sep = "", col.names = c("datetime","seq","bp1","bq1","bo1","ap1","aq1","ao1","bp2","bq2","bo2","ap2","aq2","ao2","bp3","bq3","bo3","ap3","aq3","ao3","bp4","bq4","bo4","ap4","aq4","ao4","bp5","bq5","bo5","ap5","aq5","ao5","bp6","bq6","bo6","ap6","aq6","ao6","bp7","bq7","bo7","ap7","aq7","ao7","bp8","bq8","bo8","ap8","aq8","ao8","bp9","bq9","bo9","ap9","aq9","ao9","bp10","bq10","bo10","ap10","aq10","ao10","c","price","qty","oldprice","oldqty")) df<- df[which(df$datetime != 0),] options(digits.secs = 3) df$datetime= as.posixct(df$datetime/(10^9), origin="1970-01-01", tz = "gmt")    #timestamp conversion  source('~/r/mywhich.r') source('~/nwhich.r')  #matching same line  df$change <- apply(df[, 1:62] == df[,64], 1, mywhich)  #matching "c" previous line  df$change[df[,63] == "c"] <- apply(df[which(df[,63] == "c") - 1, 1:62] == df[df[,63] == "c",64], 1, mywhich)*(-1)                                                     #matching old price previous line in "m"                                                 pos2 <- apply(df[which(df[,63] == "m") - 1, 1:62] == df[df[,63] == "m",66], 1, mywhich)                                                   #subracting 2 position in "m" df$change[df[,63] == "m"] <- df$change[df[,63] == "m"] - pos2  # arbitrary number create side df$side <- 1000  df$side[df[,63] == "m" & df[,68] == 0] <- apply(df[which(df[,63] == "m"), 1:62] == df[df[,63] == "m",64], 1, nwhich)%%2    #check -- erroneous modifications have happend outside level 5 -- might have add column #df$side[df[,63] == "m" & df[,68] != 0] <- df$change[df[,63] == "m" & df[,68] != 0]   #df$side[df[,63] == "n"] <- df$change[df[,63] == "n"] #df$side[df[,63] == "c"] <- df$change[df[,63] == "c"] df$diff <- 0 #price difference df$diff[df[,63] == "m" & df[,68] == 0] <-  df$oldprice[df[,63] == "m" & df[,68] == 0] - df$price[df[,63] == "m" & df[,68] == 0]      #askside -- price increase df$modify[df[,69] == 0 & df[,70] > 0] <- -1 #askside -- price decrease df$modify[df[,69] == 0 & df[,70] < 0] <- 1 #bidside -- price decrease df$modify[df[,69] == 1 & df[,70] < 0] <- -1 #bidside -- price increase df$modify[df[,69] == 1 & df[,70] > 0] <- 1 #copying change modify df$modify[df[,63] == "n"] <- df$change[df[,63] == "n"] df$modify[df[,63] == "c"] <- df$change[df[,63] == "c"] df$modify[df[,63] == "m" & df[,68] != 0] <- df$change[df[,63] == "m" & df[,68] != 0]  df = data.frame(time=df$datetime, modify = df$modify) finalxts <- as.xts(x = df$modify, order.by = df$time) #finalxts <- aggregatets(finalxts, fun = "sum", on = "minutes", k = 1, dropna = true) finalxts 

thank everyone.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -