(in R) how to arrange column A based on column B

62 views Asked by At

I got a data frame like this:

Factory Bread
A a
A a
B c
B b
B d
C a
D e

I want to find name of the factory with the most number of bread

I write two codes and got different answers.

1.

df %>%
  group_by(factory, bread)%>%
  summarise(n = n())%>%
  arrange(desc(n))
df %>% 
  group_by(factory) %>% 
  mutate(number = length(unique(bread)))%>% 
  arrange(desc(number))

May I ask which one is the right code and why?

Thank you!!!!

2

There are 2 answers

2
TarJae On

We could use n_distinct from dplyr package:

library(dplyr)
df %>%
    group_by(factory)%>%
    summarise(bread = n_distinct(bread))

Output:

  factory bread
  <chr>   <int>
1 A           2
2 B           1
3 C           1
0
ThomasIsCoding On

A data.table option

> setorder(setDT(df)[, .(Bread = uniqueN(Bread)), Factory], -Bread)[]
   Factory Bread
1:       B     3
2:       A     1
3:       C     1
4:       D     1