0

I am working on a big set of customer data and trying to find average number of stores a customer visit in each month. In my data I have unique identification number for each customer and the store codes they visited. Sample of my data frame looks like below:

sitecode<-c(1000,1000,1001,1000)
productcode<-c('X','X','Y','X')
customercode<-c('A','B','A','C')
Date<-c('01/01/2016','02/01/2016','03/01/2016','04/01/2016')
data1<-data.frame(customercode,Date,productcode,sitecode)

Based on this what I would like to have is a simple table for customers A-B-C with unique number of stores they visited which is 2 for A, 1 for B and C. Can you help?

  • `table(data1$customercode)` ? – mtoto Mar 09 '16 at 21:18
  • `tapply(data1$sitecode, data1$customercode, function(x) length(unique(x)))`? – Frank Mar 09 '16 at 21:23
  • @mtoto That won't work. For example customer B could visit same site twice, so table(data1$customercode) will give 2 for B. But in that particular case, since the unique number of sites customer B visits are 1, I would like to see 1. – Oğuzhan Akdeniz Mar 09 '16 at 21:26
  • I think this has your answer: http://stackoverflow.com/q/32457598/1191259 If you confirm, we can close the question. – Frank Mar 09 '16 at 21:30
  • 1
    @Frank Thank you so much, tapply(data1$sitecode, data1$customercode, function(x) length(unique(x))). It worked for me. – Oğuzhan Akdeniz Mar 09 '16 at 21:34

1 Answers1

0
data1
# customercode       Date productcode sitecode
# 1            A 01/01/2016           X     1000
# 2            B 02/01/2016           X     1000
# 3            A 03/01/2016           Y     1001
# 4            C 04/01/2016           X     1000


result=table(data1$customercode,data1$sitecode)
result

  1000 1001
A    1    1
B    1    0
C    1    0

Hope this helped to some extent.

Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30