Sami_kh
Sami_kh

Reputation: 13

Create a variable based on other factors using R

I have a data frame and I want to create a variable based on other factors, my data contains :

qi pi exep
3 300 16
2 245 14
3 300 5

I want to create the based salary

based_salary = pi+α(qi*exep)

α is between (0 and 1)

And the only accurate information about based salary is (from real data) :

Mean(based_salary) == 268

So how can I determine (α) to calculate based_salary...

Best regards,

Upvotes: 0

Views: 37

Answers (1)

Kra.P
Kra.P

Reputation: 15123

You may try

library(dplyr)
df1 <- df %>%
  mutate(x = qi * exep) %>%
  summarise(p = mean(pi),
            x = mean(x))
a = (268 - df1$p)/df1$x # = alpha : -0.4505495
df %>%
  mutate(bs = pi + a * (qi * exep)) %>%
  summarise(mean_bs = mean(bs))

  mean_bs
1     268

Upvotes: 1

Related Questions