I would like to take a vector, for example:
X <- c(1,2,3,-1,-2,-3)
and add 1 (or any other number fixed in advance) to the entries in which the values are negative, to produce
1,2,3,0,-1,-2
Is there a slick way to do this ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Submitted in the R Golf competition for this problem:
Works because (X<0) will get coerced 1 for any desired case and 0 for others.