简体   繁体   中英

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 :

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 功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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