title: generate a new factor variable depending on the values of other factors in each row
body: <p>I am trying to create a function that will generate a new variable off conditional values. I have a survey dataset with 100+ columns that will be collapsed accordingly. Read <a href="https://stackoverflow.com/questions/30172655/error-in-rowsumsx-x-must-be-an-array-of-at-least-two-dimensions-veganoec">this</a> but it did not help.</p>
  
  <pre><code>'data.frame':   117 obs. of  7 variables:
   $ fin_partner: Factor w/ 4 levels "","9","No","Yes": 2 2 4 3 2 2 2 2 4 4 ...
   $ fin_parent : Factor w/ 4 levels "","9","No","Yes": 2 2 2 2 2 2 4 3 2 2 ...
   $ fin_kids   : Factor w/ 4 levels "","9","No","Yes": 4 2 2 2 2 2 2 2 2 2 ...
   $ fin_othkids: Factor w/ 4 levels "","9","No","Yes": 2 2 2 2 2 2 3 2 2 2 ...
   $ fin_fam    : Factor w/ 4 levels "","9","No","Yes": 2 2 2 2 2 2 4 3 2 2 ...
   $ fin_friend : Factor w/ 4 levels "","9","No","Yes": 2 2 3 3 2 2 2 2 4 2 ...
   $ fin_oth    : Factor w/ 4 levels "","9","No","Yes": 2 2 2 2 2 2 2 2 4 2 ...
  </code></pre>
  
  <p>I would like to be able to subset the dataset according to columns, and then pass that through the function. Right now, the values contain "Yes", "No", "999" (for missing).</p>
  
  <p>My goal is to be able to say if, for each row, any column contains "Yes", then the new column will populate "Yes". I am sure there is an easier way than the code below, so I am open to that. </p>
  
  <p>My code currently:</p>
  
  <pre><code>trial &lt;- df[, 23:29]
  trial.test &lt;- as.data.frame(trial)
  
  composite_score &lt;- function(x){
    # Convert to numeric values
    change_to_number &lt;- function(j) {
      for (i in 1:length(j)){
        if(i == "Yes"){
          i &lt;- 1
        }
        else{
          i &lt;- 0
        }
      }
    }
  
    x &lt;- change_to_number(x)  
  
    new_col_var &lt;- function(k){
      if(rowSums(k) &gt; 0){
        k$newvar &lt;- 1
      }
      else {
        k$newvar &lt;- 0
      }
    }
  
    x &lt;- new_col_var(x)
  
  }
  
  composite_score(trial.test)
  </code></pre>
  
  <p>Code produces the following error:</p>
  
  <pre><code>Error in rowSums(k) : 'x' must be an array of at least two dimensions 
  </code></pre>
  
  <p>Data:</p>
  
  <pre><code>&gt; dput(head(trial.test))
  structure(list(fin_partner = structure(c(2L, 2L, 4L, 3L, 2L, 
  2L), .Label = c("", "9", "No", "Yes"), class = "factor"), fin_parent = structure(c(2L, 
  2L, 2L, 2L, 2L, 2L), .Label = c("", "9", "No", "Yes"), class = "factor"), 
      fin_kids = structure(c(4L, 2L, 2L, 2L, 2L, 2L), .Label = c("", 
      "9", "No", "Yes"), class = "factor"), fin_othkids = structure(c(2L, 
      2L, 2L, 2L, 2L, 2L), .Label = c("", "9", "No", "Yes"), class = "factor"), 
      fin_fam = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("", 
      "9", "No", "Yes"), class = "factor"), fin_friend = structure(c(2L, 
      2L, 3L, 3L, 2L, 2L), .Label = c("", "9", "No", "Yes"), class = "factor"), 
      fin_oth = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("", 
      "9", "No", "Yes"), class = "factor")), .Names = c("fin_partner", 
  "fin_parent", "fin_kids", "fin_othkids", "fin_fam", "fin_friend", 
  "fin_oth"), row.names = c(NA, 6L), class = "data.frame")
  </code></pre>