user5381612
user5381612

Reputation:

Product between two data.frames columns

I have two data.frames:

The first one is the coefficients of my regressions for each day:

> parametrosBase
               beta0          beta1         beta4
2015-12-15 0.1622824 -0.012956819 -0.04637442
2015-12-16 0.1641884 -0.007914548 -0.06170213
2015-12-17 0.1623660 -0.005618474 -0.05914809
2015-12-18 0.1643263  0.005380472 -0.08533237
2015-12-21 0.1667710  0.003824588 -0.09040071

The second one is: the independent (x) variables:

    > head(ir_dfSTORED)
         ind   m h0x       h1x       h4x beta0_h0x beta1_h1x beta4_h4x
1 2015-12-15  21   1 0.5642792 0.2859359         0         0         0
2 2015-12-15  42   1 0.3606713 0.2831963         0         0         0
3 2015-12-15  63   1 0.2550200 0.2334554         0         0         0
4 2015-12-15  84   1 0.1943071 0.1883048         0         0         0
5 2015-12-15 105   1 0.1561231 0.1544524         0         0         0
6 2015-12-15 126   1 0.1302597 0.1297947         0         0         0
> tail(ir_dfSTORED)
           ind    m h0x         h1x         h4x beta0_h0x beta1_h1x beta4_h4x
835 2015-12-21 2415   1 0.006799321 0.006799321         0         0         0
836 2015-12-21 2436   1 0.006740707 0.006740707         0         0         0
837 2015-12-21 2457   1 0.006683094 0.006683094         0         0         0
838 2015-12-21 2478   1 0.006626457 0.006626457         0         0         0
839 2015-12-21 2499   1 0.006570773 0.006570773         0         0         0
840 2015-12-21 2520   1 0.006516016 0.006516016         0         0         0

What i want is to multiply the beta0 column of "parametrosBase" by h0x column of "ir_dfSTORED" and store the result in the beta0_h0x column. And the same for the others: beta1 and beta4

The problem im facing is with the dates in "ind" column. This multiplication has to respect the dates.

So, once i change the day in "ir_dfSTORED" i have to change to the same day in "parametrosBase".

For example:

The first rowof "parametrosBase" df is

2015-12-15 0.1622824 -0.012956819 -0.04637442

is fixed for the 2015-12-15 day. And then i do the product. Once i enter on the 2015-12-16 day i will have to consider the second row of "parametrosBase" df.

How can i do this?

Thanks a lot. :)

Upvotes: 1

Views: 39

Answers (1)

Sam Dickson
Sam Dickson

Reputation: 5239

Maybe you should merge the two datasets first:

parametrosBase$ind <- rownames(parametrosBase)
df <- merge(ir_dfSTORED,parametrosBase)
df <- within(df,{
  beta0_h0x <- beta0*h0x
  beta1_h0x <- beta1*h0x
  beta4_h0x <- beta4*h0x
})

Since I don't know the structure of the data, you may have to convert the dates from rownames to a date format in order for the merge to work. Using ind as the name of the date in parametrosBase is key to making merge work, otherwise you'll have to specify the variables to merge by.

Upvotes: 1

Related Questions