如何根据条件语句获取两行的差异?

时间:2017-04-01 22:20:49

标签: r

我对这个非常简单的问题遇到了很多麻烦。以下是我的数据示例:

            EUR.20MA.1 EUR.LAST.2
2013-11-04          1     1.3514
2013-11-05         -1     1.3474
2013-11-06          1     1.3513
2013-11-07          0     1.3419
2013-11-08         -1     1.3367
2013-11-11          1     1.3407

基本上每当信号从1变为0或从1变为-1时,我想将该行与后续行的差异。因此,输出看起来像:

         EUR.20MA.1 EUR.LAST.2  Difference  
11/4/2013   1       1.3514      0.004
11/5/2013  -1       1.3474      0
11/6/2013   1       1.3513      0.0094
11/7/2013   0       1.3419      0   
11/8/2013  -1       1.3367      0   
11/11/2013  1       1.3407      0   
11/12/2013  1       1.3436      0   
11/13/2013  1       1.3487      0.0026
11/14/2013 -1       1.3461      0   

下面的输入:

structure(list(EUR.20MA.1 = c(1, -1, 1, 0, -1, 1, 1, 1, -1, 1
), EUR.LAST.2 = c(1.3514, 1.3474, 1.3513, 1.3419, 1.3367, 1.3407, 
1.3436, 1.3487, 1.3461, 1.3496)), .Names = c("EUR.20MA.1", "EUR.LAST.2"
), row.names = c("2013-11-04", "2013-11-05", "2013-11-06", "2013-11-07", 
"2013-11-08", "2013-11-11", "2013-11-12", "2013-11-13", "2013-11-14", 
"2013-11-15"), class = "data.frame")

3 个答案:

答案 0 :(得分:1)

使用lead包中的dplyr功能

你可以这样做:

library(dplyr)
df$Difference=df[,2]-lead(df[,2],1)
df$Difference[(2*df[,1]-lead(df[,1],1))<2]=0
# The multiplication by 2 is to differentiate between 1 followed by 0 and 0 followed by -1

答案 1 :(得分:0)

你可以在R:

中尝试这个
a <- which(df$EUR.20MA.1==1)
b <- which(df$EUR.20MA.1 %in% c(0,-1))
rws <- sapply(b, function(x) any(a==x-1))
df$Difference <- 0
df[b[rws]-1,]$Difference <- abs(df[b[rws]-1,]$EUR.LAST.2 - df[b[rws],]$EUR.LAST.2)

           # EUR.20MA.1 EUR.LAST.2 Difference
# 2013-11-04          1     1.3514     0.0040
# 2013-11-05         -1     1.3474     0.0000
# 2013-11-06          1     1.3513     0.0094
# 2013-11-07          0     1.3419     0.0000
# 2013-11-08         -1     1.3367     0.0000
# 2013-11-11          1     1.3407     0.0000
# 2013-11-12          1     1.3436     0.0000
# 2013-11-13          1     1.3487     0.0026
# 2013-11-14         -1     1.3461     0.0000
# 2013-11-15          1     1.3496     0.0000

因此,您首先找到1出现的行并将其分配给a。然后,您搜索出现0-1的行,并将其分配给b。然后在ab中搜索序列号,这些序列号是您照看的行。

或者使用这个(按照@Lamia的想法将df$EUR.20MA.1乘以2):

df$Difference <- c(head(df$EUR.LAST.2, -1) - tail(df$EUR.LAST.2, -1), 0)
df[!c(with(df, (2*head(EUR.20MA.1, -1) - tail(EUR.20MA.1, -1)) %in% c(2,3)), FALSE),] <- 0

会给你相同的结果。

答案 2 :(得分:0)

更长的dplyr版本,使用滞后而不是引导并在下一行产生结果。

library(dplyr)
df <- st %>%
  mutate(EUR.20MA.1_lag = lag(EUR.20MA.1),
         EUR.LAST.2_lag = lag(EUR.LAST.2),
         signal = (EUR.20MA.1_lag == 1 & EUR.20MA.1 == 0) |  
                  (EUR.20MA.1_lag == 1 & EUR.20MA.1 == -1),
         difference = if_else(signal, EUR.LAST.2 - EUR.LAST.2_lag, 0))

df

#    EUR.20MA.1 EUR.LAST.2 EUR.20MA.1_lag EUR.LAST.2_lag signal difference
# 1           1     1.3514             NA             NA  FALSE     0.0000
# 2          -1     1.3474              1         1.3514   TRUE    -0.0040
# 3           1     1.3513             -1         1.3474  FALSE     0.0000
# 4           0     1.3419              1         1.3513   TRUE    -0.0094
# 5          -1     1.3367              0         1.3419  FALSE     0.0000
# 6           1     1.3407             -1         1.3367  FALSE     0.0000
# 7           1     1.3436              1         1.3407  FALSE     0.0000
# 8           1     1.3487              1         1.3436  FALSE     0.0000
# 9          -1     1.3461              1         1.3487   TRUE    -0.0026
# 10          1     1.3496             -1         1.3461  FALSE     0.0000