I have a numeric vector:
a <- 1:4
# [1] 1 2 3 4
Check if values in 'a' is larger than 2:
a > 2
# [1] FALSE FALSE TRUE TRUE
However, instead of FALSE
and TRUE
, I want my result to be expressed as 0
and 1
. Currently I am using the following method.
b <- (a > 2)
b[b == TRUE] <- 1
b[b == FALSE] <- 0
Are there any simpler methods?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…