I know this should be fairly easy to fix, but for some reason I'm not getting the correct output I'm searching for. I have a large dataset where I'm trying to create a new column based on two conditions if a count column is labeled as 1 and if that row has the max payment, then create a new column where it's labeled as 1 and label the others as 0. I created a small example:
ex <- data.frame(EOC = c(1,1,2,2,2),EOC_cnt = c(1,1,1,0,0), pay = c(500,0,200,12,34))
what I want is:
ex <- data.frame(EOC = c(1,1,2,2,2),EOC_cnt = c(1,1,1,0,0), pay = c(500,0,200,12,34)),EOC_cnt1 = c(1,0,1,0,0)
I have:
out <- ex %>% group_by(EOC) %>% mutate(EOC_cnt1 = ifelse(EOC_cnt ==1 & pmax(pay) , "1", "0"))
which works on this smaller example, but when I apply it to my larger dataset I still don't get a single 1 per EOC group. Are there any other ways I could get the result I'm looking for?
(I essentially have duplicates in my EOC_cnt column and would like to create a new column where there is only a single 1 per EOC)
Here is an example of where the answer creates all 0s:
dput(ex2)
structure(list(pay = c(342.39, 48.27, 299.96, 274.12, 342.39,
121.36), EOC = c(1, 1, 1, 1, 1, 1), EOC_cnt = c(0, 1, 0, 0, 0,
0)), row.names = c(NA, -6L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x000001cdf95a1ef0>)