Off topic:how to calculate Average of purchases of distinct items in the session using dplyr
1
0
Entering edit mode
6.8 years ago

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.
dplyr R • 1.7k views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 3690 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6