I would like to create a new column esg.ordered$flowpct <- esg.ordered$flow[i]/lag(esg.ordered$size[i])
for my data frame esg
only if the value (/name) in a certain row has the same value (/name) as in the previous row in column fundid
. Otherwise the value in column flowpct should have "NA" in the respective rows. Here is my code:
for (i in esg.ordered) {
if(esg.ordered$fundid[i]==lag(esg.ordered$fundid[i],n=1)){
esg.ordered$flowpct <- esg.ordered$flow[i]/lag(esg.ordered$size[i])
}else{
esg.ordered$flowpct <- "NA"
}
}
Unfortunately, I get two mistakes:
Error in if (esg.ordered$fundid[i] == lag(esg.ordered$fundid[i], n = 1)) { : missing value where TRUE/FALSE needed
Warning: In if (esg.ordered$fundid[i] == lag(esg.ordered$fundid[i], n = 1)) { : the condition has length > 1 and only the first element will be used
Can you guys help me solving these mistakes?
Here is the data
fundid | size | flow |
---|---|---|
FS00008KNP | 78236537 | 7038075.43 |
FS00008KNP | 73048868 | -5691940.56 |
FS00008KNP | 74688822 | -193188.79 |
FS00008KNP | 95330799 | 11991514.11 |
FS00008L0W | 44170465 | -15706588.66 |
FS00008L0W | 33278560 | -12749545.90 |
FS00008L0W | 26084262 | -6879079.19 |
FS00008L0W | 23857701 | -3227825.03 |