【发布时间】:2012-01-27 20:06:07
【问题描述】:
简介
我还不是 R 专家,所以请原谅我可能应该不好意思问的另一个问题。在another question,我在stackoverflow 上询问了一些关于如何将xts 对象的不规则每日数据聚合为每周值的非常有用的cmets
apply.weekly() 函数。不幸的是,我没有找到像 tapply()、ddply()、by() 或 aggregate() 这样的函数,它允许按与 apply.weekly() 函数一起使用的类别进行拆分。
我的数据
这是我的示例数据集。我已经在另一个问题中发布了。出于说明目的,我也冒昧地在此处发布:
example <- as.data.frame(structure(c(" 1", " 2", " 1", " 2", " 1", " 1", " 2", " 1", " 2",
" 1", " 2", " 3", " 1", " 1", " 2", " 2", " 3", " 1", " 2", " 2",
" 1", " 2", " 1", " 1", " 2", NA, " 2", NA, NA, " 1", " 3", " 1",
" 3", " 3", " 2", " 3", " 3", " 3", " 2", " 2", " 2", " 3", " 3",
" 3", " 2", " 2", " 3", " 3", " 3", " 3", " 1", " 2", " 1", " 2",
" 2", " 1", " 2", " 1", " 2", " 2", " 2", " 3", " 1", " 1", " 2",
" 2", " 3", " 3", " 2", " 2", " 1", " 2", " 1", " 1", " 2", NA,
" 2", NA, NA, " 1", " 3", " 2", " 3", " 2", " 0", " 3", " 3",
" 3", " 2", " 0", " 2", " 3", " 3", " 3", " 0", " 2", " 2", " 3",
" 3", " 0", "12", " 5", " 9", "14", " 5", "tra", "tra", "man",
"inf", "agc", "07-2011", "07-2011", "07-2011", "07-2011", "07-2011"
), .indexCLASS = c("POSIXlt", "POSIXt"), .indexTZ = "", class = c("xts",
"zoo"), .indexFORMAT = "%U-%Y", index = structure(c(1297642226,
1297672737, 1297741204, 1297748893, 1297749513), tzone = "", tclass = c("POSIXlt",
"POSIXt")), .Dim = c(5L, 23L), .Dimnames = list(NULL, c("rev_sit",
"prof_sit", "emp_nr_sit", "inv_sit", "ord_home_sit", "ord_abr_sit",
"emp_cost_sit", "usage_cost_sit", "tax_cost_sit", "gov_cost_sit",
"rev_exp", "prof_exp", "emp_nr_exp", "inv_exp", "ord_home_exp",
"ord_abr_exp", "emp_cost_exp", "usage_cost_exp", "tax_cost_exp",
"gov_cost_exp", "land", "nace", "index"))))
列
“rev_sit”、“prof_sit”、“emp_nr_sit”、“inv_sit”、“ord_home_sit”、“ord_abr_sit”、“emp_cost_sit”、“usage_cost_sit”、“tax_cost_sit”、“gov_cost_sit”、“rev_exp”、 “prof_exp”、“emp_nr_exp”、“inv_exp”、“ord_home_exp”、“ord_abr_exp”、“emp_cost_exp”、“usage_cost_exp”、“tax_cost_exp”、“gov_cost_exp”、
参考调查中的问题。有“1”、“2”、“3”三种回答可能性代码。
列
“土地”、“nace”
分别是具有 16 个和 8 个独特因素的类别。
我的目标 我的目标是为“nace”和“land”中的类别因素的每个组合每周计算“1”、“2”和“3”的出现次数。我的想法是预先为每个回答可能性 {1,2,3} 创建二进制向量(example_1,example_2,example_2),然后应用类似:
apply.weekly(example_1, function(d){ddply(d,list(example$nace,example$land),sum)})
但这不适用于ddply、aggregate、by 等。
我的目标
我最初的非专业工作不是创建时间序列,而是创建一个日期向量example$date,通过%V 将给定的时间列编码为每周一次,然后使用即:
tapply(example_1[,5], list(example$date,example$nace,example$land),sum)
当然,对于上面显示的 20 个问题中的每一个,我都必须这样做。然后我得到例如example_1:
第 1 周,nace1.land1,nace1.land2,nace1.land3,...,nace1.land16,nace2.land1,..,nace8.land16 第 2 周,nace1.land1,nace1.land2,nace1.land3,...,nace1.land16,nace2.land1,...,nace8.land16 ... ... 周,nace1.land1,nace1.land2,nace1.land3,...,nace1.land16,nace2.land1,..,nace8.land16
我必须对 2 (example_2) 和 3 (example_3) 做同样的事情,这对于 20 个问题中的每一个问题都会产生所有 16*8*3*20=7680 列。这种极端情况,另外使用这种方法,产品不是时间序列,因此不能按周正确排序。
总结
所以任何人都可以教我或给我一个提示如何将函数 apply.weekly() 与 tapply()、ddply()、by()、split()、unstack() 等函数结合使用,或者任何其他方法来实现如上所述的分组。每一个提示都非常感谢。我已经在考虑放弃我的 R 实验并改回 stata 时感到很沮丧所以请帮助我!
【问题讨论】:
-
我不敢相信你在同一个句子中使用了“Stata”和“intuitive”(没有否定)......
-
@DWin:我认为这取决于你想做什么。但是在 stata 中,你有一个 by() 函数,它只是给出 sum b 一个特定索引,而不直接折叠。因此,对于一周内的所有日期,您的值都相同。
-
require(xts)错误消失。 -
@DWin:是的,example$index 这个东西只是一个变通方法。实际上我会发现使用 week.apply 会更好...
-
(删除了我关于错误的 cmets。)您应该澄清您是否希望这些计数适用于所有问题或每个问题单独。如果您使用更少的列创建测试用例,但使用同一周和 1/2/3 级别发生的一些日期和数据,这可能会有所帮助。
标签: r time-series xts categorization