简体   繁体   English

R数据输出-矩阵

[英]R data output-Matrix

date        day D1  D2 D4   D5W1 D7W2
01-01-2014      1   1   0   0   0   0
02-01-2014      2   0   1   0   0   0
03-01-2014      3   0   0   0   0   0
04-01-2014      4   0   0   1   0   0
05-01-2014     5    0   0   0   1   0
06-01-2014     6    0   0   0   0   0
07-01-2014      7   0   0   0   0   0
08-01-2014     8    0   0   0   0   0

I have a dataset till current date along with several dummy variables wherein I am doing forecasting.我有一个直到当前日期的数据集以及几个虚拟变量,我在其中进行预测。 I have a regression output where I am getting the weights for all the dummy variables which are我有一个回归输出,我得到所有虚拟变量的权重

D1      D2      D4     D5W1     D7W2
0.03    0.04    0.02    0.01    -0.05

The desired output is to generate a factor which will be generated by the multiplying weights from the regression output with the dummy variables corresponding to each date.所需的输出是生成一个因子,该因子将通过将回归输出的权重与对应于每个日期的虚拟变量相乘而生成。

date                             factor
01-01-2014  
02-01-2014  
03-01-2014  
04-01-2014  
05-01-2014  
06-01-2014  

Also a solution can be using sqldf package if data is in a data frame which is named df :如果数据位于名为df的数据框中,则解决方案也可以使用 sqldf 包:

library(sqldf)
result <- sqldf("SELECT date, 0.03 * D1 + 0.04 * D2 + 0.02 * D4 + 0.01 * D5W1 - 0.05 * D7W2 as factor
                 FROM df")

得到结果。将两个数据集从宽格式转换为长格式。使用变量作为键合并长数据集,然后将两列相乘并将其转换回宽格式。使用 value.var 的输出产品使用 dcast 功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM