In Python
, one can get the counts of values in a list by using Series.value_counts()
:
import pandas as pd
df = pd.DataFrame()
df['x'] = ['a','b','b','c','c','d']
df['y'] = list(range(1,7))
df['x'].value_counts()
c 2
b 2
a 1
d 1
Name: x, dtype: int64
In R
, I have to use three separate commands.
df <- tibble(x=c('a','b','b','c','c','d'), y=1:6)
df %>% group_by(x) %>% summarise(n=n()) %>% arrange(desc(n))
x n
b 2
c 2
a 1
d 1
Is there a shorter / more idiomatic way of doing this in R? Or am I better off writing a custom function?