0

I'm looking for an easy way to subset my df and append a column with a frequency count. Suppose I have a df like this:

Name  
JA
JN
JA
JB
JA
JN

And I want to have an outcome like this:

 Name    Frequency 
    JA      3
    JN      2
    JB      1

Any suggestion? Thank you.

2 Answers2

1

We can use tally after grouping by 'Name' with dplyr

library(dplyr)
df1 %>% 
  group_by(Name) %>%
  tally()

Or use table from base R

as.data.frame(table(df1[,1]))
#   Var1 Freq
#1   JA    3
#2   JB    1
#3   JN    2
akrun
  • 874,273
  • 37
  • 540
  • 662
0

One way to do it using data.table.

require(data.table)
DT<-data.table(df)
DT[,.(Frequency=.N),by=Name]
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58