I'm trying to develop an if else statement to apply to my data.frame below. These are the conditions I'm trying to account for:
- If the year in "Year" is = or > the larger year value in "YearLimitA" or "YearLimitB", then the value in "Value" stays the same.
- If the year in "Year" is < one of the year values in either "YearLimitA" or "YearLimitB", but > or = to the other year value in "YearLimitA" or "YearLimitB", the the value in "Value" needs to be divided by 2.
- If the year in "Year" is < both year values in "YearLimitA" and "YearLimitB", then the value in "Value" (and really the whole row) should be removed.
If you have any thoughts on the best way to do this, I would really appreciate it. I'm not sure where to start when there are multiple conditions.
Example df:
Name <- c("t1", "t1", "t1", "t1", "t2", "t2", "t2", "t3", "t3", "t3", "t3")
Value <- c(1.7, 2.6, 3.2, 4.1, 1.8, 3.4, 2.4, 3.6, 4.0, 1.9, 2.3)
Year <- c(2000, 2001, 2002, 2003, 2001, 2002, 2003, 2000, 2001, 2002, 2003)
YearLimitA <- c(2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 2002, 2002, 2002)
YearLimitB <- c(2002, 2002, 2002, 2002, 2002, 2002, 2002, 2001, 2001, 2001, 2001)
df <- data.frame(Name, Value, Year, YearLimitA, YearLimitB)
Intended df after if else statement:
Name <- c("t1", "t1", "t1", "t2", "t2", "t3", "t3", "t3")
Value <- c(1.3, 3.2, 4.1, 3.4, 2.4, 2.0, 1.9, 2.3)
Year <- c(2001, 2002, 2003, 2002, 2003, 2001, 2002, 2003)
YearLimitA <- c(2001, 2001, 2001, 2002, 2002, 2002, 2002, 2002)
YearLimitB <- c(2002, 2002, 2002, 2002, 2002, 2001, 2001, 2001)
df2 <- data.frame(Name, Value, Year, YearLimitA, YearLimitB)