Search code examples
rvariablescreation

Creating new variable from three existing variables in R


I have a dataset that looks like the one below, and I would like to create a new variable based on these variables, which can be used with the other variables in the dataset.

The first variable, ID, is a respondent identification number. The med variable are 1 and 2, indicating different treatments. Var1_v1 and Var1_v2 has four real options 1,2,3, or 9, and these options are only given to those who med ==1. If med ==2, NA appears in the Var1s. Var2 receives NA when med ==1 and has real values ranging from 1-3 when med==2.

ID <- c(1,2,3,4,5,6,7,8,9,10,11)
med <- c(1,1,1,1,1,1,2,2,2,2,2)
Var1_v1 <- c(2,2,3,9,9,9,NA,NA,NA,NA,NA) #ranges from 1-3, and 9
Var1_v2 <- c(9,9,9,1,3,2,NA,NA,NA,NA,NA) #ranges from 1-3, and 9
Var2 <- c(NA,NA,NA,NA,NA,NA,3,3,1,3,2)

#tables to show you what data looks like relative to med var
table(Var1_v1, med)
table(Var1_v2, med)
table(Var2, med)

I've been looking around for a while to figure out a recoding/new variable creation code, but I have had no luck.

Ultimately, I would like to create a new variable, say Var3, based on three conditions:

  • Uses the values from Var1_v1 if the value = 1, 2, or 3
  • Uses the values from Var1_v2 if the value = 1, 2, or 3
  • uses the values from Var2 if the values = 1, 2, or 3

And this variable should be able to match up with the ID number, so that it can be used within the dataset.

So the final variable should look like:

 Var3 <- (2,2,3,1,3,2,3,3,1,3,2)

Thanks!


Solution

  • Something like

    v <- Var1_v1
    v[Var1_v2 %in% 1:3] <- Var1_v2[Var1_v2 %in% 1:3]
    v[Var2 %in% 1:3] <- Var2[Var2 %in% 1:3]
    v
    [1] 2 2 3 1 3 2 3 3 1 3 2
    

    which uses one of them as a base (you could also use a pure NA vector) and simply fills in only parts that match.