title: counting occurrences in column and create variable in R
body: <p>I am new on R and I have a data.frame , called "CT", containing a column called "ID" containing several hundreds of different identification numbers (these are patients). Most numbers appear once, but some others appear two or three times (therefore, in different rows). 
  In the CT data.frame, I would like to insert a new variable, called "countID", which would indicate the number of occurrences of these specific patients (multiple records should still appear several times).
  I tried two different strategies after reading this forum: 
  1st strategy:</p>
  
  <pre><code>CT &lt;- cbind(CT, countID=sequence(rle(CT.long$ID)$lengths)
  </code></pre>
  
  <p>But this doesn't work, I get only one count. 
  2nd strategy: create a data frame with two columns (one is ID, one is count) and the match this dataframe with CT: </p>
  
  <pre><code>tabs &lt;- table(CT.long$ID)
  out &lt;- data.frame(item=names(unlist(tabs)),count=unlist(tabs)[],stringsAsFactors=FALSE)
  rownames(out) = c()
  head(out)
  
  # item    count
  # 1 1.312     1
  # 2 1.313     2
  # 3 1.316     1
  # 4 1.317     1
  # 5 1.321     1
  # 6 1.322     1
  </code></pre>
  
  <p>So this works fine but I can't melt the two data.frames: the number of rows doesn't match between "out" and "CT" (out has less rows of course). 
  Maybe someone has an elegant solution to add the number of occurrences directly in the data.frame CT, or correctly match the two data.frames?</p>