0

I have table below:

Account.Code    Brand       
1600    Sensodyne TP        
1600    Sensodyne TP        
1600    Zem     
1600    Sensodyne TP        
1800    Sensodyne TP        
1800    Burb MN     
1800    Burb MN     
2000    Lami        
2000    Lami        

How do I transform to table like below:

Account.Code    Brand   Count
1600    Sensodyne TP    3
1600    Zem             1
1800    Sensodyne TP    1
1800    Burb MN         2
2000    Lami            2

I would need to do this a much larger data set, shown is just a simple example.

Hassan
  • 41
  • 3
  • add a reproducible example (see [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)) and I can help you. Something that we can copy & paste to our R-section, for finding your solution. Tip: using `data.table` it's pretty straightforward. – David Leal Mar 13 '17 at 14:35
  • `data.frame(table(df))` should do it for this problem. – lmo Mar 13 '17 at 18:51

2 Answers2

0

Using dplyr:

library(dplyr)

df %>%
  group_by(Account.Code, Brand) %>%
    summarise(count = n())

#  Account.Code        Brand count
#         <int>       <fctr> <int>
#1         1600 Sensodyne_TP     3
#2         1600          Zem     1
#3         1800      Burb_MN     2
#4         1800 Sensodyne_TP     1
#5         2000         Lami     2
J_F
  • 9,956
  • 2
  • 31
  • 55
0

Or a base R solution

df$Count = 1
aggregate(Count ~ Account.Code + Brand, data=df, FUN=length)
  Account.Code        Brand Count
1         1800      Burb MN     2
2         2000         Lami     2
3         1600 Sensodyne TP     3
4         1800 Sensodyne TP     1
5         1600          Zem     1
G5W
  • 36,531
  • 10
  • 47
  • 80