calculating average of each session by unique itemID
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.