11

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?

max
  • 4,141
  • 5
  • 26
  • 55

1 Answers1

15

The tidyverse has dplyr::count, which is a shortcut for 'group_by' and 'summarize' to get counts.

df <- tibble(x=c('a','b','b','c','c','d'), y=1:6)

dplyr::count(df, x, sort = TRUE)

# A tibble: 4 x 2
  x         n
  <chr> <int>
1 b         2
2 c         2
3 a         1
4 d         1

rpolicastro
  • 1,265
  • 8
  • 14