Let´s say I have a dataframe, df. This df has 3 columns: Names, D and R.
Now, R has been filled with a constant value for each name, but only accordingly to the maximum value that D has. The problem is that this is not the case: variable R actually depends on the value that D has, and as I said, the existing value of R is only correct for the highest number of D for each name.
I want to decrease the value of R depending on the value of D for each name. To be more precise, each time D decreases by -200 (these are mts, but it´s not important), R has to decrease by -0.1. This functional relation applies to each and every name: the only relevant fact according to names is that each names has it´s own "starting point" for R.
Names D R
1 Group1 3290 1.4
2 Group2 3129 1.6
3 Group1 2920 1.4
4 Group4 1100 1.9
5 Group1 3500 1.4
6 Group1 3323 1.4
...
As you can see, the value of R is constant for each group, even though D is changing. Let´s say 3500 is the highest value for Group1. Then, I would expect something like:
Names D R
1 Group1 3290 1.3
2 Group2 3129 1.6
3 Group1 2920 1.2
4 Group4 1100 1.9
5 Group1 3500 1.4
6 Group1 3323 1.4
...
I already coded a solution for this:
library(dplyr)
df <- df %>% mutate(R = case_when(
(Names=="Group1"& D>=3500-100) ~ 1.4
(Names=="Group1"& D<3500-100) ~ 1.3
(Names=="Group1"& D<3500-200) ~ 1.2
...
(Names=="GroupN"& D>=#highest_value-100) ~ #default_value_of_R_for_GroupN
...
))
But this is not elegant. That´s why I tried another solution like:
library(sqldf)
list_Names <- sqldf("SELECT DISTINCT NAMES FROM df")
n<-1
while (n<30) {#here, 30 is arbitrary
decrement = n*200
for (r in df$R) {
for (n in list_names) {
if (df$names[r]==n & df$D<max(df$D) - decrem) {
r = (r - n*0.1)
}}}
n = n+1
}
But that doesn´t go anywhere :/
I know there is a much simpler way to proceed here. Any help (R or Python) would be highly appreciated!!!
P.D: the default values for R are not necessarily the max values, I have them in an Excel file.