Reputation: 569
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.
Upvotes: 0
Views: 184
Reputation: 49260
One way to do it using data.table
.
require(data.table)
DT<-data.table(df)
DT[,.(Frequency=.N),by=Name]
Upvotes: 0
Reputation: 887118
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
Upvotes: 1