Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding tally(sort = TRUE)

Tags:

r

dplyr

So there was this code before:

flights %>%
  group_by(dest) %>%
  summarise(arr_delay = mean(arr_delay, na.rm = TRUE),
  n = n()) %>%
arrange(desc(arr_delay))

This code I understand. However this code right below that one shows:

flights %>%
  group_by(carrier, flight, dest) %>%
  tally(sort = TRUE) %>% # Save some typing
  filter( n == 365)

So this code I don't get the

tally(sort = TRUE)

When it said save some typing, what exactly is it saving? I understand that the tally(sort = TRUE) replaces summerise(n = n()), but how does it "save typing" and how does it relate to each other? If anyone can give me a break down of tally(sort = TRUE) that'd be greatly appreciated!

like image 886
user3784616 Avatar asked Jul 20 '14 18:07

user3784616


1 Answers

I'm far from being a dplyr expert, but since no one wants to answer, I'll give it a shot. So from tally documentation all it does is just giving you the frequencies per group. If you embed two tallys, they will just sum the frequencies, so for example:

library(dplyr)
tally(group_by(CO2, Plant)) 

#    Plant n
# 1    Qn1 7
# 2    Qn2 7
# 3    Qn3 7
# 4    Qc1 7
# 5    Qc3 7
# 6    Qc2 7
# 7    Mn3 7
# 8    Mn2 7
# 9    Mn1 7
# 10   Mc2 7
# 11   Mc3 7
# 12   Mc1 7

is just base R table

table(CO2$Plant)
# Qn1 Qn2 Qn3 Qc1 Qc3 Qc2 Mn3 Mn2 Mn1 Mc2 Mc3 Mc1 
#   7   7   7   7   7   7   7   7   7   7   7   7 

and

tally(tally(group_by(CO2, Plant)))
#    n
# 1 84

is just

sum(table(CO2$Plant))
# [1] 84

or

tally(CO2)
#   n
#1 84

or

nrow(CO2)
# [1] 84

So re your question,

flights %>%
  group_by(carrier, flight, dest) %>%
  tally(sort = TRUE) %>% # Save some typing
  filter( n == 365)

means

Take data set "flights" 
 group it by "carrier", "flight" and "dest" columns
 give me the frequencies of these combinations and sort them by frequecy
 return only the combinations that their frequency equals to 365
like image 51
David Arenburg Avatar answered Sep 29 '22 18:09

David Arenburg