Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating new variable from three existing variables in R
    text
    copied!<p>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. </p> <p>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.</p> <pre><code>ID &lt;- c(1,2,3,4,5,6,7,8,9,10,11) med &lt;- c(1,1,1,1,1,1,2,2,2,2,2) Var1_v1 &lt;- c(2,2,3,9,9,9,NA,NA,NA,NA,NA) #ranges from 1-3, and 9 Var1_v2 &lt;- c(9,9,9,1,3,2,NA,NA,NA,NA,NA) #ranges from 1-3, and 9 Var2 &lt;- 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) </code></pre> <p>I've been looking around for a while to figure out a recoding/new variable creation code, but I have had no luck.</p> <p>Ultimately, I would like to create a new variable, say Var3, based on three conditions:</p> <ul> <li>Uses the values from Var1_v1 if the value = 1, 2, or 3</li> <li>Uses the values from Var1_v2 if the value = 1, 2, or 3</li> <li>uses the values from Var2 if the values = 1, 2, or 3</li> </ul> <p>And this variable should be able to match up with the ID number, so that it can be used within the dataset.</p> <p>So the final variable should look like:</p> <pre><code> Var3 &lt;- (2,2,3,1,3,2,3,3,1,3,2) </code></pre> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload