I am trying to analyze my data to compare db_perk by plan. I want to make a column that takes the db_perk divided by the db_perk of the first plan in the plan column. This way I can see the differences of db_perk depending on plan.
I want to take this data called SQL_Table:
plan gender marital_status accel_type extension_type inflation iss_age dur db_perk
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
1 BasicF F Married A.24 E.0 AC3.EC3 40 1 0.20
2 BasicF F Married A.24 E.0 AC3.EC3 40 2 0.25
3 BasicF F Married A.24 E.0 AC3.EC3 40 3 0.30
4 BasicF F Married A.24 E.0 AC3.EC3 40 4 0.40
5 BasicF M Single A.36 E.24 AC3.EC3 40 1 0.15
6 GradedF F Married A.24 E.0 AC3.EC3 40 1 0.25
7 GradedF F Married A.24 E.0 AC3.EC3 40 2 0.30
8 GradedF F Married A.24 E.0 AC3.EC3 40 3 0.50
9 GradedF F Married A.24 E.0 AC3.EC3 40 4 0.70
10 GradedF M Single A.36 E.24 AC3.EC3 40 1 0.10
And transform it to this:
plan gender marital_status accel_type extension_type inflation iss_age dur db_perk db_perk_compare
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 BasicF F Married A.24 E.0 AC3.EC3 40 1 0.20 1.00
2 BasicF F Married A.24 E.0 AC3.EC3 40 2 0.25 1.00
3 BasicF F Married A.24 E.0 AC3.EC3 40 3 0.30 1.00
4 BasicF F Married A.24 E.0 AC3.EC3 40 4 0.40 1.00
5 BasicF M Single A.36 E.24 AC3.EC3 40 1 0.15 1.00
6 GradedF F Married A.24 E.0 AC3.EC3 40 1 0.25 1.25
7 GradedF F Married A.24 E.0 AC3.EC3 40 2 0.30 1.20
8 GradedF F Married A.24 E.0 AC3.EC3 40 3 0.50 1.67
9 GradedF F Married A.24 E.0 AC3.EC3 40 4 0.70 1.75
10 GradedF M Single A.36 E.24 AC3.EC3 40 1 0.10 0.67
As you can see the db_perk_compare column is = "1" when the plan is "BasicF" because the formula is dividing the db_perk by BasicF's db_perk. The other columns can also have multiple different values that would effect db_perk.
I've tried something like this
for (i in nrow(SQL_Table)){
SQL_Table$db_perk_compare[i] <- SQL_Table$db_perk[i]/SQL_Table$db_perk[which(plan == SQL_Table$plan[1],
gender == SQL_Table$gender[i],
marital_status == SQL_Table$marital_status[i],
accel_type == SQL_Table$accel_type[i],
extension_type == SQL_Table$extension_type [i],
inflation == SQL_Table$inflation [i],
iss_age == SQL_Table$iss_age[i],
dur == SQL_Table$dur[i])]
}
but get this error:
Error in which(plan == SQL_Table$plan[1], gender == SQL_Table$gender[i], :
unused arguments (accel_type == SQL_Table$accel_type[i], extension_type == SQL_Table$extension_type[i], inflation == SQL_Table$inflation[i], iss_age == SQL_Table$iss_age[i], dur == SQL_Table$dur[i])