-5

I am using R and have a data frame as such

Record_Num    STR
1             ABC
1             ABC
1             EFG
2             HIJ
2             HIJ
2             KLM
3             KLM
3             KLM
3             KLM

I am attempting to get a count for each STR for each record number, so something like this:

Record_Num    STR    Count
1             ABC    2
1             EFG    1
2             HIJ    2
2             KLM    1
3             KLM    3

If I can get that I can take the steps to get what I need, but my next step is to simply weed out the max count for each STR (which I will then use to create a loop - I'm trying to tidy up some very messy data). If I can incorporate that into a single step, all the better, but I'm not concerned about that. I just can't seem to solve the above problem. Given that I'm an extreme novice. I've toyed around with table and tapply, but haven't been able to get it right. Can anybody help?

I realize this is very basic for most of you, so sorry for such a novice question.

fleetmack
  • 349
  • 1
  • 3
  • 11
  • This question has been asked numerous times [here](http://stackoverflow.com/questions/7450600/count-number-of-observations-rows-per-group-and-add-result-to-data-frame) and [here](http://stackoverflow.com/questions/25293045/count-number-of-rows-in-a-data-frame-in-r-based-on-group), for instance. – Uwe Nov 30 '16 at 22:03

3 Answers3

2

Or even simpler:

library(dplyr)

count(dat, Record_Num, STR)
denrou
  • 630
  • 3
  • 12
1

You can do the following:

require(dplyr)
dat %>% 
  group_by(Record_Num, STR) %>% 
  summarise(Count = n())

Which gives you:

  Record_Num    STR Count
1          1    ABC     2
2          1    EFG     1
3          2    HIJ     2
4          2    KLM     1
5          3    KLM     3
Rentrop
  • 20,979
  • 10
  • 72
  • 100
1

If you use a data.table this is very easy.

setDT(dat) 
dat[,.(Count =.N), by=.(STR, Record_Num)]
Kristofersen
  • 2,736
  • 1
  • 15
  • 31
  • 1
    This also works perfectly (if I add the record_num to the group by clause), thanks for the correct & fast response! – fleetmack Nov 30 '16 at 21:56