문제

I have a one-column xts object:

a <- c(1,1,1,2,3,2,2,2,2,1,0,0,0,0,2,3,4,4,1,1)
date <- Sys.Date()-20:1
data <- xts(a,date)
colnames(data) <- "a"
data

Here I want all the numbers in the column a to be replaced by +1 and then -1 respectively, except 0. I want the a column to look like:

1,-1,1,-1,1,-1,1,-1,1,-1,0,0,0,0,1,-1,1,-1,1,-1

I've asked similar questions, but this is not a exact duplicate.

도움이 되었습니까?

해결책

Assuming that your data frame is named df. This will repeat values 1 and -1 for all a that are not 0.

df[a!=0,]<-c(1,-1)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top