0

How can I create a new column that represents the count of another column in my dataframe using dplyr?

For example:

library(dplyr)

d <- tibble(x = c("a", "a", "b", "c", "c", "c"))
# A tibble: 6 x 1
  x    
  <chr>
1 a    
2 a    
3 b    
4 c    
5 c    
6 c 

I understand I can do this with a join, but can I do it without a join?

d %>%
  left_join(count(d, x), by = "x")
# A tibble: 6 x 2
  x         n
  <chr> <int>
1 a         2
2 a         2
3 b         1
4 c         3
5 c         3
6 c         3
Brigadeiro
  • 2,649
  • 13
  • 30

0 Answers0