根据组计算R中数据帧中的行数 [英] count number of rows in a data frame in R based on group

查看:553
本文介绍了根据组计算R中数据帧中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个数据框,如下所示:

  ID MONTH-YEAR VALUE 
110 JAN。 2012 1000
111 JAN。 2012 2000
。 。
。 。
121 FEB。 2012 3000
131 FEB。 2012 4000
。 。

所以,每年的每个月都有 n 行,它们可以是任何顺序(意味着它们都不连续并且处于休息状态)。我想计算每个 MONTH-YEAR 的行数,即JAN有多少行。 2012年,FEB有多少。 2012等等。如下所示:

  MONTH-YEAR NUMBER OF ROWS 
JAN。 2012 10
FEB。 2012 13
MAR 2012 6
APR。 2012 9

我试图这样做:

  n_row<  -  nrow(dat1_frame%。%group_by(MONTH-YEAR))

但它不会产生所需的输出。我该怎么做?

解决方案

这显示了如何 table(。)(或者更接近匹配您想要的输出, data.frame(table(。))做你想要的东西。



还要注意如何分享可重复的样本数据,使其他人可以复制和粘贴到他们的会话中。 p>

这是(可重现的)样本数据:

  mydf<  - 结构(列表(ID = c(110L,111L,121L,131L,141L),
MONTH.YEAR = c(JAN。2012,JAN。2012,
FEB。2012 FEB。2012,
MAR。2012),
VALUE = c(1000L,20 00L,3000L,4000L,5000L)),
.Names = c(ID,MONTH.YEAR,VALUE),
class =data.frame,row.names = c(NA,-5L))

mydf
#ID MONTH.YEAR VALUE
#1 110 JAN。 2012 1000
#2 111 JAN。 2012 2000
#3 121 FEB。 2012 3000
#4 131 FEB。 2012 4000
#5 141 MAR 2012 5000

这里是每组的行数,以两种输出显示格式计算: p>

 表(mydf $ MONTH.YEAR)

#FEB。 2012年1月2012 MAR 2012
#2 2 1

data.frame(table(mydf $ MONTH.YEAR))
#Var1 Freq
#1 FEB。 2012 2
#2 JAN。 2012 2
#3 MAR。 2012 1


I have a data frame in R like this:

  ID   MONTH-YEAR   VALUE
  110   JAN. 2012     1000
  111   JAN. 2012     2000
         .         .
         .         .
  121   FEB. 2012     3000
  131   FEB. 2012     4000
         .           .
         .           .

So, for each month of each year there are n rows and they can be in any order(mean they all are not in continuity and are at breaks). I want to calculate how many rows are there for each MONTH-YEAR i.e. how many rows are there for JAN. 2012, how many for FEB. 2012 and so on. Something like this:

 MONTH-YEAR   NUMBER OF ROWS
 JAN. 2012     10
 FEB. 2012     13
 MAR. 2012     6
 APR. 2012     9

I tried to do this:

n_row <- nrow(dat1_frame %.% group_by(MONTH-YEAR))

but it does not produce the desired output.How can I do that?

解决方案

Here's an example that shows how table(.) (or, more closely matching your desired output, data.frame(table(.)) does what it sounds like you are asking for.

Note also how to share reproducible sample data in a way that others can copy and paste into their session.

Here's the (reproducible) sample data:

mydf <- structure(list(ID = c(110L, 111L, 121L, 131L, 141L), 
                       MONTH.YEAR = c("JAN. 2012", "JAN. 2012", 
                                      "FEB. 2012", "FEB. 2012", 
                                      "MAR. 2012"), 
                       VALUE = c(1000L, 2000L, 3000L, 4000L, 5000L)), 
                  .Names = c("ID", "MONTH.YEAR", "VALUE"), 
                  class = "data.frame", row.names = c(NA, -5L))

mydf
#    ID MONTH.YEAR VALUE
# 1 110  JAN. 2012  1000
# 2 111  JAN. 2012  2000
# 3 121  FEB. 2012  3000
# 4 131  FEB. 2012  4000
# 5 141  MAR. 2012  5000

Here's the calculation of the number of rows per group, in two output display formats:

table(mydf$MONTH.YEAR)
# 
# FEB. 2012 JAN. 2012 MAR. 2012 
#         2         2         1

data.frame(table(mydf$MONTH.YEAR))
#        Var1 Freq
# 1 FEB. 2012    2
# 2 JAN. 2012    2
# 3 MAR. 2012    1

这篇关于根据组计算R中数据帧中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆