I would need to create a new column in my data, which would be equal to 'tender' value in case the 'id' appears only once, and to the 'lot' value in case it does not. I cannot do it through anything concerning NA, since the data is incomplete and there is a lot of NAs in there. My idea was to do it that if 'id' is unique, then select
df <- data.frame('id'=c(1,1,2,3,3,4),
'lot'=c(10,20,NA,40,50,NA), 'tender'=c(30,30,30,90,90,40))
A am expecting the output to be:
data.frame('id'=c(1,1,2,3,3,4), 'lot'=c(10,20,NA,40,50,NA),
'tender'=c(30,30,30,90,90,40),'price'=c(10,20,30,40,50,40))
df %>% mutate(price = coalesce(lot, tender))df %>% group_by(id) %>% mutate(price = case_when(n() ==1 ~ tender, TRUE ~ lot))