for()循环中的嵌套ifelse()...语法错误?

时间:2014-06-10 13:39:38

标签: r if-statement for-loop nested

相当具体的问题......但是在这里。

我有一个指示热变化的数据向量,并且它想知道某些东西是否已经切换为“开启”或“关闭”。这个东西总是开始'关闭'。 +1.5度的阈值表示它已经被切换为“ON”,并且保持开启直到-1.5度的阈值表示它已经再次切换为“OFF”,在更小的+/-变化之间达到高或低的平台。我有一个正值和负值的数值向量,我想把它变成一个在适当的位置有“ON”和“OFF”等级的字符或因子。

使用此:

    > Data$delta
    [1]  0.02  0.00  0.04 -0.06 -0.06 -0.02  0.01  0.31  0.22  0.21 -0.09 -0.02  0.03  0.02  0.01
    [16]  0.00  0.02  0.03  0.03  0.04  0.04  0.01  0.00  0.01  0.02  0.05  0.04  0.04  0.01  0.04
    [31]  0.02  0.01 -0.03  0.00  0.03  0.03  0.03  0.04  0.04  0.02  0.02  0.01  0.02 -0.02 -0.01
    [46] -0.03  0.01  0.03  0.37  0.14  0.04 -0.34 -0.15 -0.07  0.00  0.01  0.29  0.03  0.26 -0.12
    [61]  0.05 -0.02 -0.03  0.10 -0.11 -0.01 -0.07 -0.03 -0.01  0.01  0.30  0.12  0.05 -0.25 -0.06
    [76] -0.04 -0.04 -0.07 -0.02  0.01  0.04  0.02  0.03 -0.07 -0.12 -0.18 -0.12 -0.08 -0.05 -0.04
    [91]  0.34  2.99  4.29  5.00  1.83 -0.51 -1.63 -0.33  2.62 -0.38

我创建一个空向量来接收for()循环输出并插入'OFF',因为开关始终以'OFF'开头

    > Data$switch<- as.character(NA)
    > Data$switch<-as.character(c("OFF",Data$switch[2:(length(Data$switch))]))
    > head(Data$switch
    [1] "OFF" NA    NA    NA    NA    NA

然后我为()循环创建嵌套的ifelse(),说明Data $ switch的第1行已经完成......

    > for(i in (2:(length(Data$delta))))
    {  ifelse(Data$switch[1:((length(Data$switch))-1)] == "OFF", 
       (ifelse (Data$delta[i] < 1.5 , Data$switch[i] <- "OFF", Data$switch[i] <- "ON" )),
       (ifelse (Data$delta[i] > {-1.5} , Data$switch[i] <- "ON", Data$switch[i] <- "OFF")))
    }

返回......

> Data$switch
[1] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[16] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[31] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[46] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[61] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[76] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[91] "OFF" "ON"  "ON"  "ON"  "ON"  "ON"  "OFF" "ON"  "ON"  "ON"

这一切看起来都很好......除了值98,它不应该只用'-0.33'切换到'ON'。在较大的数据集上,这些似乎是从OFF到ON的常规错误变化,值<1.5?我已经以多种形式再次尝试了语法,但这些情况更糟糕并不是更好。任何人都可以帮助确定问题吗?数据$ delta是一个数字向量。

2 个答案:

答案 0 :(得分:1)

行。以下是更多复制/粘贴友好格式的delta值

delta <- c(0.02, 0, 0.04, -0.06, -0.06, -0.02, 0.01, 0.31, 0.22, 0.21, 
-0.09, -0.02, 0.03, 0.02, 0.01, 0, 0.02, 0.03, 0.03, 0.04, 0.04, 
0.01, 0, 0.01, 0.02, 0.05, 0.04, 0.04, 0.01, 0.04, 0.02, 0.01, 
-0.03, 0, 0.03, 0.03, 0.03, 0.04, 0.04, 0.02, 0.02, 0.01, 0.02, 
-0.02, -0.01, -0.03, 0.01, 0.03, 0.37, 0.14, 0.04, -0.34, -0.15, 
-0.07, 0, 0.01, 0.29, 0.03, 0.26, -0.12, 0.05, -0.02, -0.03, 
0.1, -0.11, -0.01, -0.07, -0.03, -0.01, 0.01, 0.3, 0.12, 0.05, 
-0.25, -0.06, -0.04, -0.04, -0.07, -0.02, 0.01, 0.04, 0.02, 0.03, 
-0.07, -0.12, -0.18, -0.12, -0.08, -0.05, -0.04, 0.34, 2.99, 
4.29, 5, 1.83, -0.51, -1.63, -0.33, 2.62, -0.38)

这是获取所需数据的一种方法。首先,我跟踪何时出现“on”和“off”标志并将初始标志设置为off(off = 1,on = 2)

flags <- ifelse(delta< -1.5, 1, ifelse(delta>1.5, 2, NA))
flags[1]<-1

现在这个向量有很多缺失值。我需要做的就是向前推进最后一个价值。我会用

做到这一点
state <- Reduce(function(a,b) {ifelse(is.na(b),a,b)}, flags, accumulate=T)
c("OFF","ON")[state]

答案 1 :(得分:0)

    > for(i in (2:(length(Data$delta))))
{  ifelse(Data$switch[i-1] == "OFF", 
   (ifelse (Data$delta[i] < 1.5 , Data$switch[i] <- "OFF", Data$switch[i] <- "ON" )),
   (ifelse (Data$delta[i] > {-1.5} , Data$switch[i] <- "ON", Data$switch[i] <- "OFF")))
}

我相信我解决了这个问题!感谢人们的其他解决方案