r - Converting a vector with numerical and NA values to a vector with binary values -
i want convert vector negative values (-inf,0]
, na
0
, positive values (0,inf)
same new vector binary variables in r use in logistic regression.
more specifically, data set med
consists of matrix vector can retrieved med$amount_total
. vector consists of numbers ranging -10,000 100,000 na values. want convert these vector 0s in place of negative, 0 , na values , convert positive values 1s use in glm (logistic regression model) i'm going fit other vectors in med.
example:
input <- c(50,na,-4,32,0,0,12) desired_output <- c(1,0,0,1,0,0,1)
we can try
as.integer(replace(v1, is.na(v1)|v1 < 0, 0)!=0)
or
as.integer(!(is.na(v1) | v1 <= 0)) #[1] 1 0 0 1 0 0 1
Comments
Post a Comment