R:计算组中观察值的数量

编程入门 行业动态 更新时间:2024-10-10 05:26:30
本文介绍了R:计算组中观察值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用R编程语言,我尝试在此处继续本教程:在R

Using the R programming language, I am trying to follow this tutorial over here: Count number of observations per day, month and year in R

我每天创建一次数据,然后每周对这些数据进行汇总.到"y.week"文件,我想添加一个计数"列,列出了每周的观察次数.

I create data at daily intervals and then took weekly sums of this data. To the "y.week" file, I want to add a "count" column that lists the number of observations in each week.

这是我正在使用的以下代码:

Here is the code below I am using:

#load libraries library(xts) library(ggplot2) #create data date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day") date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d") property_damages_in_dollars <- rnorm(731,100,10) final_data <- data.frame(date_decision_made, property_damages_in_dollars) #aggregate and count by week y.week <-aggregate(property_damages_in_dollars~format(as.Date(date_decision_made), format="%W-%y"),data=final_data, FUN=sum) counts_week <- data.frame(table(as.Date(index(y.week)))) y.week$count = count_week

但是我认为这是不正确的.

But I don't think this is correct.

然后我尝试每月做一次相同的事情:

I then tried to do the same thing per month:

#aggregate and count by month y.mon<-aggregate(property_damages_in_dollars~format(as.Date(date_decision_made), format="%Y/%m"),data=final_data, FUN=sum) counts_mon <- data.frame(table(as.Date(index(y.mon)))) y.mon$count = count_mon

通常,我会使用"dplyr"图书馆按组进行计数(按月计数,按周计数),但是我不确定如何讲"书目.dplyr会将同一周(或同一月)中的观察结果视为组".

Normally, I would have used the "dplyr" library to count by group (count by month, count by week), but I am not sure how to "tell" dplyr to consider observations in the same week (or in the same month) as a "group".

有人可以告诉我我在做什么错吗?

Can someone please tell me what I am doing wrong?

谢谢

可能的答案(由Ronak Shah提供):

Possible answer (provided by Ronak Shah) :

每周:

date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day") date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d") property_damages_in_dollars <- rnorm(731,100,10) final_data <- data.frame(date_decision_made, property_damages_in_dollars) final_data %>% mutate(date_decision_made = as.Date(date_decision_made)) %>% group_by(week = format(date_decision_made, "%W-%y")) %>% summarise( total = sum(property_damages_in_dollars, na.rm = TRUE), Count = n())

每月:

date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day") date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d") property_damages_in_dollars <- rnorm(731,100,10) final_data <- data.frame(date_decision_made, property_damages_in_dollars) final_data %>% mutate(date_decision_made = as.Date(date_decision_made)) %>% group_by(week = format(date_decision_made, "%Y-%m")) %>% summarise( total = sum(property_damages_in_dollars, na.rm = TRUE), Count = n())

推荐答案

如果将对象保持其自然形式会更好.例如,将日期保留为日期而不是字符串.然后,您可以使用

It would be better if you keep objects in their natural form. For example, keeping dates as dates instead of string. You can then use

library(dplyr) final_data %>% mutate(date_decision_made = as.Date(date_decision_made)) %>% add_count(week = format(date_decision_made, "%W-%y"), name = 'Count')

使用 add_count 是将 group_by + mutate 与 n()结合使用的快捷方式:

Using add_count is a shortcut over using group_by + mutate with n() :

final_data %>% mutate(date_decision_made = as.Date(date_decision_made)) %>% group_by(week = format(date_decision_made, "%W-%y")) %>% mutate(Count = n())

更多推荐

R:计算组中观察值的数量

本文发布于:2023-11-22 02:10:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615566.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   数量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!