Niki
Niki

Reputation: 23

Conditional recoding of variables in R with creation of extra variable

I am new to R and in need of help. I have to recode variables as follows:

recode into two variables, V11 and V12, then standardize both.

if old V11 value = 0, new V11 value = 0 and V12 value = 0
if old V11 value = 1, new V11 value = 1
if old V11 value = 2, new V11 value = 1 and V12 value = 1

I am struggling to create a working piece of code. Thank you for your help.

Upvotes: 0

Views: 435

Answers (1)

user16051136
user16051136

Reputation:

if (V11==0) {
 V12 <- 0
}
else if (V11==2) {
 V11 <- 1
 V12 <- 1
}

This solves the problem if V11 and V12 are just length 1. Since the second condition in your code doesn't actually change anything, it doesn't need to be included in the answer.

If V11 and V12 are instead vectors with length>1, try:

V12[V11==0] <- 0
V12[V11==2] <- 1
V11[V11==2] <- 1

Upvotes: 1

Related Questions