I have some data that looks like the following:
UniqueID MonthYear Count2 CCount
402ABC OCT2018 5 1
402ABC NOV2018 5 2
402ABC DEC2018 5 3
402ABC JAN2019 5 4
402ABC FEB2019 5 5
495DEF FEB2019 10 1
495DEF MAR2019 10 2
495DEF APR2019 10 3
495DEF MAY2019 10 4
495DEF JUN2019 10 5
495DEF AUG2019 10 6
495DEF SEP2019 10 7
495DEF DEC2019 10 8
495DEF JAN2020 10 9
495DEF FEB2020 10 10
What I want is to be able to create a new column titled "Corrected" that finds the last instance of a UniqueID. Ideally, the new data would look like the following:
UniqueID MonthYear Count2 CCount Corrected
402ABC OCT2018 5 1 0
402ABC NOV2018 5 2 0
402ABC DEC2018 5 3 0
402ABC JAN2019 5 4 0
402ABC FEB2019 5 5 1
495DEF FEB2019 10 1 0
495DEF MAR2019 10 2 0
495DEF APR2019 10 3 0
495DEF MAY2019 10 4 0
495DEF JUN2019 10 5 0
495DEF AUG2019 10 6 0
495DEF SEP2019 10 7 0
495DEF DEC2019 10 8 0
495DEF JAN2020 10 9 0
495DEF FEB2020 10 10 1
I had a series of nested ifelse functions used to create the "Corrected" column in my data frame. The nested chunk looked like the following:
smd$Corrected=ifelse(smd$MonthYear=="OCT2018" & smd$Count2==1, 1,ifelse(
smd$MonthYear=="OCT2018" & smd$Count2 >1, 0,ifelse(
smd$MonthYear=="NOV2018" & smd$Count2==1, 1,ifelse(
smd$MonthYear=="NOV2018" & smd$Count2==smd$CCount, 1,ifelse(
smd$MonthYear=="NOV2018" & smd$Count2!=smd$CCount, 0,ifelse(
smd$MonthYear=="DEC2018" & smd$Count2==1, 1,ifelse(
smd$MonthYear=="DEC2018" & smd$Count2==smd$CCount, 1,ifelse(
smd$MonthYear=="DEC2018" & smd$Count2!=smd$CCount, 0,ifelse(
………………………
smd$MonthYear=="MAY2020" & smd$Count2==1, 1,ifelse(
smd$MonthYear=="MAY2020" & smd$Count2==smd$CCount, 1,ifelse(
smd$MonthYear=="MAY2020" & smd$Count2!=smd$CCount, 0,0
)))))))))))))))))))))))))))))))))))))))))))))))))))))
I understand this was not the most elegant solution, but it worked for my purposes. This is just one part of a larger analysis I was running. The analysis was updated once every month and I added a new chunk to this code each time I updated it. Today, when I attempted to update the code, I got the following error:
Error: contextstack overflow at line 66
Doing a little bit of research, I learned that this error pops up when you use too many nested ifelse statements. Other questions on StackOverflow suggested I use either sapply or mapply to rewrite this code. I tried a couple of apply functions here to rewrite my code, but nothing seemed to work quite right.
Can anyone help suggesting how I would rewrite my code to not use all the nested ifelse functions? Any help would be appreciated!
Thank you!
Aucun commentaire:
Enregistrer un commentaire