Reputation: 367
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 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)
Upvotes: 0
Views: 2577
Reputation: 11514
Try this:
df <- df[df$Year >= pmin(df$YearLimitA, df$YearLimitB),]
df$Value <- with(df, ifelse(Year>=pmax(YearLimitA, YearLimitB), Value, Value/2))
First, we only keep rows in which Year
is at least as big as the minimum of YearLimitA
and YearLimitB
. Then, if Year
is at least as big as the pairwise maximum of those two variables, then the value is retained. If not, it is divided by 2.
This yields
Name Value Year YearLimitA YearLimitB
2 t1 1.3 2001 2001 2002
3 t1 3.2 2002 2001 2002
4 t1 4.1 2003 2001 2002
6 t2 3.4 2002 2002 2002
7 t2 2.4 2003 2002 2002
9 t3 2.0 2001 2002 2001
10 t3 1.9 2002 2002 2001
11 t3 2.3 2003 2002 2001
and all.equal(df2, df, check.attributes=F)
gives TRUE
.
Upvotes: 1
Reputation: 81733
Here's one approach:
# sort years
rangeYear <- apply(df[c("YearLimitA", "YearLimitB")], 1, range)
# remove colums
idx <- df$Year >= rangeYear[1, ]
df2 <- df[idx, ]
# change values
df2 <- transform(df2, Value = Value / (1 + (Year < rangeYear[2, ][idx])))
The result:
Name Value Year YearLimitA YearLimitB
2 t1 1.3 2001 2001 2002
3 t1 3.2 2002 2001 2002
4 t1 4.1 2003 2001 2002
6 t2 3.4 2002 2002 2002
7 t2 2.4 2003 2002 2002
9 t3 2.0 2001 2002 2001
10 t3 1.9 2002 2002 2001
11 t3 2.3 2003 2002 2001
Upvotes: 1