R:矢量化条件

时间:2016-03-17 08:31:22

标签: r for-loop vectorization

我有一个包含超过一百万行和67列的数据集。我创建了一个新列,根据下面的代码记录分数。

我陷入了需要照顾的状况:

DF$change[DF[,63] == "M"] <- DF$change[DF[,63] == "M"] - pos2

当我DF$change[DF[,63] == "M"] - pos2 = 0时,我需要查看是否在竞价方或询问方发生了这种情况。我可以通过查看DF [,64]的位置是偶数(askside)还是奇数(bidside)的方式确定。有一点需要注意,我无法使用我已经计算过的pos2的值来执行此操作,因为我的函数名为mywhich(如果需要,我可以提供代码)在代码中使用。 所以要确定偶数/奇数,我必须重新计算DF [,64]的位置。一旦我知道偶数或奇数,DF $变化应该是-1或1,这取决于DF [,66]&gt; DF [,64]或&lt;。

现在,我已尝试过子集,但我不知道这是如何起作用的,因为我重新计算了这些位置。我尝试不使用mywhich这个部分,但我似乎无法理解它使它工作。

任何指针/建议?我还应该尝试什么?我应该写一个单独的函数来处理这个吗?写另一个版本的哪个功能?我有点失落

这是我到目前为止所做的:

 > 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

这是输出:

> 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")

向下滚动以查找dput(DF)

1 个答案:

答案 0 :(得分:0)

我终于明白了这一点,我想我可以创建尽可能多的新列,以便计算我所拥有的内容,以便我做了什么。我认为这是最好的方法,但如果有其他方式甚至更快,我愿意接受建议。

这是我的代码:

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 with same line for all 
DF$change <- apply(DF[, 1:62] == DF[,64], 1, mywhich)

#matching "C" with 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 with previous line in "M"                                                
pos2 <- apply(DF[which(DF[,63] == "M") - 1, 1:62] == DF[DF[,63] == "M",66], 1, mywhich)                                                 

#subracting the two position in "M"
DF$change[DF[,63] == "M"] <- DF$change[DF[,63] == "M"] - pos2

# arbitrary number to 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 this -- erroneous for modifications that have happend outside level 5 -- might have to add another column for this
#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 to 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

感谢大家的帮助。