基于组中数据框中的数据框中的行数
中有一个数据框
ID MONTH-YEAR VALUE
110 JAN. 2012 1000
111 JAN. 2012 2000
. .
. .
121 FEB. 2012 3000
131 FEB. 2012 4000
. .
. .
我在r
连续性并处于休息状态)。我想计算每个一个月年度
的行几行,即JAN有多少行。 2012年,2月有多少。 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))
但是它不会产生所需的输出。我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
count()
plyr
做您想要的工作:The
count()
function inplyr
does what you want:这是一个示例,显示
table(。)
(或更紧密地匹配您所需的输出,data.frame(table(。))
做听起来像您是的事 注意请
。
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:
Here's the calculation of the number of rows per group, in two output display formats:
尝试在dplyr中使用计数函数:
我不确定您是如何作为变量名称的一个月。我的R版本不允许这样的变量名称,因此我用一个月代替了它。
附带说明,您的代码中的错误是
dat1_frame%。%group_by(一个月年)
没有总结
函数将返回原始数据框架而没有任何修改。所以,你想使用Try using the count function in dplyr:
I am not sure how you got MONTH-YEAR as a variable name. My R version does not allow for such a variable name, so I replaced it with MONTH.YEAR.
As a side note, the mistake in your code was that
dat1_frame %.% group_by(MONTH-YEAR)
without asummarise
function returns the original data frame without any modifications. So, you want to use使用Ananda虚拟升级的示例数据集,这是一个示例,使用
gengregate()
,它是core R. core R.gengregate()
仅需要一些东西来计数为月份
的不同值。在这种情况下,我使用value
作为计数的东西:它为您提供了。
Using the example data set that Ananda dummied up, here's an example using
aggregate()
, which is part of core R.aggregate()
just needs something to count as function of the different values ofMONTH-YEAR
. In this case, I usedVALUE
as the thing to count:which gives you..
只是为了完成数据。表解决方案:
Just for completion the data.table solution:
如果“一个月年度”是一个变量,这将为您提供答案。
首先,尝试唯一(数据$月),看看它是否返回唯一值(无重复)。
然后,在简单的拆分竞争中,将返回您要寻找的内容。
This will give you the answer, if "MONTH-YEAR" is a variable.
First, try unique(data$MONTH-YEAR) and see if it returns unique values (no duplicates).
Then above simple split-apply-combine will return what you are looking for.
这是使用
汇总
按组计数行的另一种方法:Here is another way of using
aggregate
to count rows by group:假设我们有以下DF_DATA
框
数据 noreferrer“>
摘要函数将从因子参数创建一个表,然后为结果创建一个向量(第7行&amp; 8)
Suppose we have a df_data data frame as below
To count number of rows in df_data grouped by MONTH-YEAR column, you can use:
summary function will create a table from the factor argument, then create a vector for the result (line 7 & 8)