title: R Loop over unique values in a dataframe column to create another one based on conditions
body: <p>My dataset consists of scores and total respondents for questions asked in a survey, over a number of fiscal years (FY13, FY14 &amp; FY15) and in different regions.</p>
  
  <p>My objective is to loop through the <code>FY</code> column and identify when each question was asked, for each region. And store this information in a new column.</p>
  
  <p>This is what a reproducible sample looks like -</p>
  
  <pre><code>testdf=data.frame(FY=c("FY13","FY14","FY15","FY14","FY15","FY13","FY14","FY15","FY13","FY15","FY13","FY14","FY15","FY13","FY14","FY15"),
                Region=c(rep("AFRICA",5),rep("ASIA",5),rep("AMERICA",6)),
                QST=c(rep("Q2",3),rep("Q5",2),rep("Q2",3),rep("Q5",2),rep("Q2",3),rep("Q5",3)),
                Very.Satisfied=runif(16,min = 0, max=1),
                Total.Very.Satisfied=floor(runif(16,min=10,max=120)),
                Satisfied=runif(16,min = 0, max=1),
                Total.Satisfied=floor(runif(16,min=10,max=120)),
                Dissatisfied=runif(16,min = 0, max=1),
                Total.Dissatisfied=floor(runif(16,min=10,max=120)),
                Very.Dissatisfied=runif(16,min = 0, max=1),
                Total.Very.Dissatisfied=floor(runif(16,min=10,max=120)))
  </code></pre>
  
  <p>I start with creating an ID column, by concatenating <code>Region</code> &amp; <code>QST</code></p>
  
  <pre><code>library(tidyr)
  testdf = testdf %&gt;%
  unite(ID,c('Region','QST'),sep = "",remove = F)
  </code></pre>
  
  <p><strong>My Objective</strong></p>
  
  <p>1) For each unique <code>ID</code>, identify whether the given question was asked -</p>
  
  <p>a) Only on one year (either FY13, FY14 or FY15)</p>
  
  <p>b) Over the Past Two Years (FY15 &amp; FY14 only)</p>
  
  <p>c) Over the Past Three Years (FY15 &amp; FY14 &amp; FY13)</p>
  
  <p>d) On FY13 &amp; FY15 Only</p>
  
  <p><strong>My Attempt</strong></p>
  
  <p>For this problem, I tried to create a <code>for loop</code>, and for each unique <code>ID</code>, I first store the unique occurences of each FY the question was asked in a vector <code>v</code>. Then using an IF conditional statement I assign a comment to a newly created column called <code>Tally</code> based on these occurences.</p>
  
  <pre><code>for (i in unique(testdf$ID))
  {
  v=unique(testdf$FY)
  
    if(('FY15' %in% v) &amp; ('FY14' %in% v)) {
        testdf$Tally=='Asked Over The Past Two Years'
    } 
    else if(('FY15' %in% v) &amp; ('FY14' %in% v) &amp; ('FY13' %in% v)) {
         testdf$Tally=='Asked Over The Past Three Years'
    }
    else if(('FY13' %in% v) &amp; ('FY15' %in% v)) {
          testdf$Tally=='Question Asked in FY13 &amp; FY15 Only'
    }
    else { testdf$Tally=='Question Asked Once Only' 
    }
  
  }  
  </code></pre>
  
  <p>The loop seems to run without throwing an error message, but it doesn't seem to create the new <code>Tally</code> column.</p>
  
  <p>Any help with this will be greatly appreciated.</p>