【问题标题】:Understanding tally(sort = TRUE)了解计数(排序 = TRUE)
【发布时间】:2014-07-20 18:29:53
【问题描述】:

原来有这样的代码:

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

这个代码我明白了。然而,下面的这段代码显示:

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

所以这段代码我没有得到

tally(sort = TRUE)

当它说保存一些输入时,它到底保存了什么?我知道tally(sort = TRUE) 取代了summerise(n = n()),但它如何“节省打字”以及它们之间的关系如何?如果有人能给我tally(sort = TRUE) 的详细信息,将不胜感激!

【问题讨论】:

  • “保存输入”对我来说意味着它可以节省一些输入,而不必输入summarise(和后续)行。不?另外,还有一个tally的帮助文件
  • 这是它唯一保存的东西吗?它可以节省打字摘要吗?我只是想确保运算符“tally”没有其他应用程序
  • 你从哪里得到这个代码?是不是来自哈德利的用户!教程?
  • ^是的!学习 R 很难.. :(
  • ^感谢您的建议!适当注意:)

标签: r dplyr


【解决方案1】:

我远不是dplyr 专家,但由于没有人愿意回答,我会试一试。因此,从tally documentation 开始,它所做的只是为您提供每组的频率。如果您嵌入两个tallys,它们将只是sum 频率,例如:

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

只是基础 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 

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

只是

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

tally(CO2)
#   n
#1 84

nrow(CO2)
# [1] 84

所以你的问题,

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

意思

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    相关资源
    最近更新 更多