Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 循环中的子集_R_Dataframe_Subset - Fatal编程技术网

R 循环中的子集

R 循环中的子集,r,dataframe,subset,R,Dataframe,Subset,我有一个关于日期、产品类别、商店和数量的表格。大约有100种产品类型(产品1到产品100)和30种商店类型(商店1到商店30)。对于每个产品商店组合,我需要准备一个时间序列模型。你能帮我快速准备这些产品组合的子集吗。提前谢谢!样本数据包括在下面 datekey Product Store Quantity 20150320 Product29 Store24 1500000 20150110 Product20 Store17 941266 201

我有一个关于日期、产品类别、商店和数量的表格。大约有100种产品类型(产品1到产品100)和30种商店类型(商店1到商店30)。对于每个产品商店组合,我需要准备一个时间序列模型。你能帮我快速准备这些产品组合的子集吗。提前谢谢!样本数据包括在下面

datekey    Product       Store   Quantity
20150320    Product29   Store24  1500000
20150110    Product20   Store17  941266 
20160331    Product29   Store12  770000
20160331    Product20   Store25  130000
20150503    Product84   Store20  97117
20160331    Product20   Store6   13000
20160331    Product29   Store21  200
20160331    Product29   Store28  193
20160331    Product29   Store22  180
20160331    Product20   Store23  171
20160331    Product29   Store9   165
20160331    Product9    Store23  160
20160331    Product29   Store6   139
20160331    Product20   Store17  134

#This what I have tried for one column, but need help for multiple cols
stest <- split(sales, sales$Store, drop = FALSE)
datekey产品存储数量
20150320产品29仓库24 1500000
20150110产品20仓库17 941266
20160331产品29仓库12 770000
20160331产品20仓库25 130000
20150503产品84仓库20 97117
20160331产品20仓库6 13000
20160331产品29仓库21 200
20160331产品29仓库28 193
20160331产品29仓库22 180
20160331产品20商店23 171
20160331产品29仓库9 165
20160331产品9商店23 160
20160331产品29仓库6 139
20160331产品20仓库17 134
#这是我为一个专栏所做的尝试,但为多个col需要帮助

stest您可以使用
tidyr
unite
这两列

df %>% unite(joined, c(Product, Store))

# datekey            joined Quantity
# 1  20150320 Product29_Store24  1500000
# 2  20150110 Product20_Store17   941266
# 3  20160331 Product29_Store12   770000
# 4  20160331 Product20_Store25   130000
# 5  20150503 Product84_Store20    97117
# 6  20160331  Product20_Store6    13000
# 7  20160331 Product29_Store21      200
# 8  20160331 Product29_Store28      193
# 9  20160331 Product29_Store22      180
# 10 20160331 Product20_Store23      171
# 11 20160331  Product29_Store9      165
# 12 20160331  Product9_Store23      160
# 13 20160331  Product29_Store6      139
# 14 20160331 Product20_Store17      134
样本数据中有14个不同的产品/门店组

df %>% unite(joined, c(Product, Store)) %>% n_distinct(.$joined)

# [1] 14

按组运行时间序列回归(
joined
)。如果您在分析后需要将它们拆分回来,请使用
separate

如果没有一些特定的数据和您试图实现的目标的示例,很难提供帮助。@lebelinoz我已经添加了数据的图像。看看它是否有帮助。如果您可以对数据使用
dput
命令,并编辑您的问题以包含输出,那就更好了。这是一个巨大的数据,可以为不同的产品和商店类型推断样本数据。我已经尝试过拆分函数'stest@lebelinoz-看看现在是否有帮助。谢谢谢谢你,凯文。不过,还有一个稍微不同的问题。现在,如果我必须按照“已加入”从每日到每周汇总销售额,您建议我应该怎么做。您可以将
datekey
转换为适当的日期类型,然后以周为单位对结果进行汇总way@Vishal,以延续我先前的评论。您可以使用
df%>%mutate(weekkey=format(datekey,“%Y-W%V”)
创建一个week列。这将在样本数据的第一行显示
2015-W12
,这意味着根据ISO 8601,这是一年中的第12周。