We can recreate your data using:
df <- tibble::tibble(
ID = c(rep(1,7),rep(2,8)),
Comp = c(rep("A",7),rep("B",8)),
Prod = c(
"X","Y","Y","Z",
"X","Y","Z",
"E","F","G","E",
"F","G","F","G"
),
FY = c(
2009,2009,2010,2010,
2011,2011,2011,
2009,2009,2009,2010,
2010,2010,2011,2011
),
`Performance (P)` = c(
1.1,1.1,1.2,1.2,
1.3,1.3,1.3,
0.5,0.5,0.5,0.7,
0.7,0.7,0.8,0.8
)
)
We can subset ONLY the information we need using dplyr. We will call this subset dictionary
.
dictionary <- dplyr::select(
df,
Comp, FY, `Performance (P)`
)
dictionary <- unique(dictionary)
Then we can create a function that looks up this information for each row in df
.
fxn <- function(company, fy, performance, dictionary){
new_values <- sapply(
seq_along(company),
function(i){
# Attempt to extract the previous year's value.
prev_performance <- dplyr::filter(
dictionary,
Comp == company[i],
FY == fy[i] - 1
)[["Performance (P)"]]
# Return the new value or the current year's value.
if(length(prev_performance) == 0){
return(performance[i])
}else{
return(pi - prev_performance)
}
}
)
new_values
}
Finally, we can use the function mutate
to apply this function to each row of df
.
dplyr::mutate(
df,
`Aspiration (Asp)` = fxn(Comp, FY, `Performance (P)`, dictionary)
)
I hope this helps!