calculating average of each session by unique itemID i have a data frame 

 &gt; data
session id        item id 
  1                 1 
  1                 1
  1                 1
  2                 1
  2                 2
  3                 1
  3                 0
  3                 3
  3                 2


and i want to calculate avg of each session id like this uniqueitemsinsession/totalitems= ans (1/3=0.333)and get result like this

&gt;  result   
session id       avg
   1             0.333
   2             1.000
   3             1.000


how can i do this??
Assuming you can use dplyr package

data %&gt;% group_by(session_id) %&gt;% summarise(avg = length(unique(item_id))/length(item_id))


You have asked many questions on basic grouping and summarise operations in R. It would be better to go through tutorials like this to learn all these tricks in one go.