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)
Question
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.
Solution
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)