I keep encountering a problem in my R code with generating a new variable based upon another variable. Every participant has entries for multiple different variables. Not all of these variables matter for each participant. I have a dummy coded variable which I use to tell me which variable I should use when generating my new variable. Here is what my data would look like.
data
id use v1 v2 v3
1 1 2 2 1
2 2 NA 1 2
3 3 1 NA 3
4 1 3 5 NA
5 2 4 4 1
I will try to create a new variable using the dummy coded variable. For this example, is use is 1, I want to use the value of v1 for x. If use is 2, then I want to use v2 for x. If use is 3, I want to use v3 for x. Here is the code I use.
data$x [data$use == 1] <- data$v1
data$x [data$use == 2] <- data$v2
data$x [data$use == 3] <- data$v3
When I try to run the code, I will then get the error message saying "number of items to replace is not a multiple of replacement length".
I did some research and I think this has something to do with data being missing (though I could be wrong). I tried to use is.na () within the [] but this does not solve the issue.
I have used ifelse to solve problems similar to this before, but I don't think that code would work in this circumstance because I have more than two situations (I am not sure if ifelse is cumulative or not).
Why does this error occur and what is the best way to resolve this?