Entering edit mode
5.6 years ago
shameenkhan075
•
0
I have a data frame with 3 columns: session id, item id and class. In the class column, 0 represents 'not purchased' and 1 represents 'purchased'.
> data
session id item id class
1 1 0
1 1 0
1 1 0
2 1 1
2 2 0
3 1 0
3 0 1
3 3 1
3 2 0
I would like to to calculate the average number of purchases of distinct items for each unique session id. in SessionID 1 contain only one unique item and 0 purchase so avg is 0/1=0 session id 2 contain 1 purchase and 2 unique items so avg will be 1/2=0.5 and session id 3 contain 2 purchases and 4 unique items so avg will be 2/4=0.5. The results would look like this:
> result
session id avg
1 0/1=0
2 0.5
3 0.5
I have tried this till now:
data %>% group_by(session_id) %>% summarise(avg = ifelse(length(Class==1))/length(unique(item_id)))
but got the error:
Error in summarise_impl(.data, dots) :
Evaluation error: argument "yes" is missing, with no default.