首页 文章

计算每个组中的行数

提问于
浏览
82

我有一个数据帧,我想计算每组内的行数 . 我定期使用 aggregate 函数对数据求和如下:

df2 <- aggregate(x ~ Year + Month, data = df1, sum)

现在,我想计算观察但似乎无法找到 FUN 的正确参数 . 直觉上,我认为它会如下:

df2 <- aggregate(x ~ Year + Month, data = df1, count)

但是,没有这样的运气 .

有任何想法吗?


一些玩具数据:

set.seed(2)
df1 <- data.frame(x = 1:20,
                  Year = sample(2012:2014, 20, replace = TRUE),
                  Month = sample(month.abb[1:3], 20, replace = TRUE))

12 回答

  • 52

    还有 df2 <- count(x, c('Year','Month')) (plyr包)

  • 43

    按照@Joshua 's suggestion, here'的方法,您可以计算 df 数据框中 Year = 2007和 Month = 11月(假设它们是列)的观测数量:

    nrow(df[,df$YEAR == 2007 & df$Month == "Nov"])
    

    以及_GregSnow之后的 aggregate

    aggregate(x ~ Year + Month, data = df, FUN = length)
    
  • 14

    我们也可以使用 dplyr .

    首先,一些数据:

    df <- data.frame(x = rep(1:6, rep(c(1, 2, 3), 2)), year = 1993:2004, month = c(1, 1:11))
    

    伯爵:

    library(dplyr)
    count(df, year, month)
    #piping
    df %>% count(year, month)
    

    我们也可以使用稍长版本的管道和 n() 功能:

    df %>% 
      group_by(year, month) %>%
      summarise(number = n())
    

    tally 功能:

    df %>% 
      group_by(year, month) %>%
      tally()
    
  • 27

    没有 data.table 解决方案的旧问题 . 所以这里......

    使用 .N

    library(data.table)
    DT <- data.table(df)
    DT[, .N, by = list(year, month)]
    
  • 29

    aggregate 一起使用的简单选项是 length 函数,它将为您提供子集中向量的长度 . 有时候更健壮的是使用 function(x) sum( !is.na(x) ) .

  • 16

    为每行创建一个值为1的新变量 Count

    df1["Count"] <-1
    

    然后聚合数据帧,由 Count 列求和:

    df2 <- aggregate(df1[c("Count")], by=list(year=df1$year, month=df1$month), FUN=sum, na.rm=TRUE)
    
  • -1

    在这种情况下, aggregate() 函数的替代方法是 table() ,带有 as.data.frame() ,这也表示年份和月份的哪些组合与零次出现相关联

    df<-data.frame(x=rep(1:6,rep(c(1,2,3),2)),year=1993:2004,month=c(1,1:11))
    
    myAns<-as.data.frame(table(df[,c("year","month")]))
    

    并且没有零发生的组合

    myAns[which(myAns$Freq>0),]
    
  • 21

    对于我的聚合,我通常最终希望看到平均值和“这个群体有多大”(长约一小时) . 所以这是我在这些场合的便利片段;

    agg.mean <- aggregate(columnToMean ~ columnToAggregateOn1*columnToAggregateOn2, yourDataFrame, FUN="mean")
    agg.count <- aggregate(columnToMean ~ columnToAggregateOn1*columnToAggregateOn2, yourDataFrame, FUN="length")
    aggcount <- agg.count$columnToMean
    agg <- cbind(aggcount, agg.mean)
    
  • 2

    使用 sqldf 包的sql解决方案:

    library(sqldf)
    sqldf("SELECT Year, Month, COUNT(*) as Freq
           FROM df1
           GROUP BY Year, Month")
    
  • 0

    考虑到@Ben的答案,如果 df1 不包含 x 列,则R会抛出错误 . 但它可以用 paste 优雅地解决:

    aggregate(paste(Year, Month) ~ Year + Month, data = df1, FUN = NROW)
    

    同样,如果在分组中使用两个以上的变量,则可以进行推广:

    aggregate(paste(Year, Month, Day) ~ Year + Month + Day, data = df1, FUN = NROW)
    
  • 0

    您可以使用 by 函数作为 by(df1$Year, df1$Month, count) ,它将生成所需聚合的列表 .

    输出看起来像,

    df1$Month: Feb
         x freq
    1 2012    1
    2 2013    1
    3 2014    5
    --------------------------------------------------------------- 
    df1$Month: Jan
         x freq
    1 2012    5
    2 2013    2
    --------------------------------------------------------------- 
    df1$Month: Mar
         x freq
    1 2012    1
    2 2013    3
    3 2014    2
    >
    
  • 4
    lw<- function(x){length(which(df$variable==someValue))}
    
    agg<- aggregate(Var1~Var2+Var3, data=df, FUN=lw)
    
    names(agg)<- c("Some", "Pretty", "Names", "Here")
    
    View(agg)
    

相关问题