使用仅包含ISO周的数据集将ISO周汇总为几个月 [英] Aggregate ISO weeks into months with a dataset containing just ISO weeks

查看:83
本文介绍了使用仅包含ISO周的数据集将ISO周汇总为几个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据位于具有如下结构的数据框中:

My data is in a dataframe which has a structure like this:

df2 <- data.frame(Year = c("2007"), Week = c(1:12), Measurement = c(rnorm(12, mean = 4, sd = 1)))

不幸的是,我没有每个测量的完整日期(例如,缺少日期),只有年份和星期(这些是ISO周)。

Unfortunately I do not have the complete date (e.g. days are missing) for each measurement, only the Year and the Weeks (these are ISO weeks).

现在,我想将一个月的测量值的中位数(例如,特定年份每月的每周测量值)汇总到一个新列月中。如果没有确切的可用日期,我找不到一种便捷的方法来执行此操作。

Now I want to aggregate the Median of a Month's worth of measurements (e.g. the weekly measurements per month of the specific year) into a new column, Months. I did not find a convenient way to do this without having the exact day of the measurements available. Any inputs are much appreciated!

推荐答案

当需要将一周分配给一个月时,尽管ISO 8601并未考虑这种情况,但可能会应用一年的第一周。 Wikipedia

例如,2007年的第5周属于2月,因为第5周的星期四是2月1日。

For example, the 5th week of 2007 belongs to February, because the Thursday of the 5th week was the 1st of February.

我正在使用 data.table ISOweek 软件包。请参见示例如何计算星期几。然后您可以按月进行任何汇总。

I am using data.table and ISOweek packages. See the example how to compute the month of the week. Then you can do any aggregation by month.

require(data.table)
require(ISOweek)

df2 <- data.table(Year = c("2007"), Week = c(1:12),
                  Measurement = c(rnorm(12, mean = 4, sd = 1)))

# Generate Thursday as year, week of the year, day of week according to ISO 8601
df2[, thursday_ISO := paste(Year, sprintf("W%02d", Week), 4, sep = "-")]

# Convert Thursday to date format
df2[, thursday_date := ISOweek2date(thursday_ISO)]

# Compute month
df2[, month := format(thursday_date, "%m")]
df2

Uwe建议计算年月字符串。

Suggestion by Uwe to compute a year-month string.

# Compute year-month
df2[, yr_mon := format(ISOweek2date(sprintf("%s-W%02d-4", Year, Week)), "%Y-%m")]
df2

最后,您可以对新表进行汇总或通过将中位数作为列来添加。

And finally you can do an aggregation to the new table or by adding median as a column.

df2[, median(Measurement), by = yr_mon]

df2[, median := median(Measurement), by = yr_mon]
df2

这篇关于使用仅包含ISO周的数据集将ISO周汇总为几个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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