title: How to assign the value of a vector to a data frame?
body: <p>I have a vector of IDs currently as factors. I have a for loop that checks for those IDs in a data frame and returns a particular value. I am creating a data frame that stores in column 1 the ID currently being run through the loop and in the second column the value of interest.</p>
  
  <p>The problem I am having is when assigning the <em>ith</em> ID to my data frame, it is returning the factor's index number and not the value. See code.</p>
  
  <pre><code>ref &lt;- unique(yearsd[,11]) # yearsd df has customer records; i'm extracting unique IDs
  counter &lt;- data.frame(matrix(ncol = 2, nrow = length(ref))) # initialize counter for for loop
  
  for(i in 1:length(ref))
  {
    loc &lt;- which(ref[i] == yearsd[,11]) # returns positions of IDs
    yearTF &lt;- unique(yearsd[loc,3])     # gives me a vector of years that ID shows up
    counter[i,1] = print(ref[i])        # store the ID currently in the loop
    counter[i,2] = length(yearTF)       # store the number of years the show up in the records
  }
  </code></pre>
  
  <p>If the <em>ith</em> element of <em>ref</em> is ABCD and is the 32nd level of the factor, my <em>counter[i,1]</em> value ends up being 32 instead of ABCD. I also tried <code>print(ref[i])</code> but had no luck with that either. I always get the level's index number of the factor.</p>
  
  <p>Would it be better if I just change it to character? They are alphanumeric strings.</p>
  
  <h2>Edit</h2>
  
  <ul>
  <li>yearsd is a df with customer records</li>
  <li>yearsd[,11] contains the customer ID</li>
  <li>for each record, there is a transaction date, which stores only the
  year, e.g. 2005, 2006, etc.</li>
  </ul>
  
  <p>I am trying to go through yearsd to get a df containing those customer IDs in one column and a count of how many years they had transactions in the second column/</p>
  
  <pre><code>Example Output:
  
  CustID   YearsIn
  A0001    3
  D504     1
  RR45Y    2
  </code></pre>
  
  <p>Meaning customer A0001 had transactions in 3 different years, D504 had transactions in only 1 year, and RR45Y had transactions in 2 different years. Each customer may have multiple transactions in a year. I only care to know if they had at least 1; if so, I count that year for that customer.</p>
  
  <p>Let me know if you have any questions. I appreciate the help.</p>