Nick Knauer
Nick Knauer

Reputation: 4243

Multiplying same column names from different dataframes

I have two dataframes: one containing the original values, and the other containing the coefficients. I want to create a new dataframe that does the following calculation: standard_deviation * coefficient_of_variable * variable_value. I then want to attach the string _decomp at the end of these new values.

df1

Standard_Deviation    Var1     Var2    Var3   Intercept
                10       4        5       9           1 
                10      12        7      11           1
                10      25        9       3           1
                10      12        8       1           1

df2

    names      coefficients
Intercept             -1.25
     Var1              0.08
     Var2              1.08
     Var3             -0.04

I want my final output to look like this:

       Var1_decomp     Var2_decomp    Var3_decomp   Intercept_decomp
       3.2                      54           -3.6              -12.5 
       9.6                    75.6           -4.4              -12.5
       20                     97.2           -1.2              -12.5
       9.6                    86.4           -0.4              -12.5

Upvotes: 0

Views: 44

Answers (1)

mickey
mickey

Reputation: 2188

Make the data frames

df1 = read.table(text="Standard_Deviation    Var1     Var2    Var3   Intercept
                10       4        5       9           1 
                10      12        7      11           1
                10      25        9       3           1
                10      12        8       1           1", header = TRUE)

df2 = read.table(text="names      coefficients
Intercept             -1.25
     Var1              0.08
     Var2              1.08
     Var3             -0.04", header = TRUE)

You can process in this way.

out = matrix(0, nrow = length(df1$Standard_Deviation), ncol = NCOL(df1) - 1)

for (i in 2:NCOL(df1)){
    j = which(df2$names == names(df1)[i])
    out[,i-1] = df1[,1] * df1[,i] * df2[j,2]
    }

colnames(out) = paste0(names(df1)[-1], "_decomp")

Upvotes: 2

Related Questions