如果我用一个字符替换一些值,data[data<0] <- "Down"
然后通过:data[data>0] <- "Up"
我将所有值都设置为“Up”,但是如果我反转替换,我会以我喜欢的方式工作。
data<-runif(30, min=-5, max=5)
data[data<0]<-"Down"
data[data>0]<-"Up"
#[1] "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up"
#[16] "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up" "up"
但如果我这样做,我会得到正确的结果:
data<-runif(30, min=-5, max=5)
data[data>0]<-"Up"
data[data<0]<-"Down"
#[1] "down" "up" "up" "down" "down" "down" "down" "down" "down" "down"
#[11] "down" "down" "up" "down" "down" "down" "up" "up" "down" "up"
#[21] "up" "down" "up" "up" "down" "down" "up" "up" "down" "down"
解决方案很简单,做第二个,但我很好奇为什么会发生这种情况。起初我以为这与转换为字符有关,但不可能是这样,因为那样更改顺序不会影响或会以相同的方式影响。任何想法?