I have a dataframe like the following:
ID <- c(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5)
Type <- c('A','B','C','B', 'C','A','B', 'C','A','C', 'C')
Value <- c(10, 11, 12, 1, 2, 100, 101, 102, -1, -2, -10)
df <- data.frame(ID, Type, Value)
My goal is a dataframe, which only has one row for each ID. But I would like to select the rows based on certain criteria / waterfall principle.
So type A is preferred, then type B, then type C. My goal is the following dataframe:
ID <- c(1, 2, 3, 4, 5)
Type <- c('A','B','A','A', 'C')
Value <- c(10, 1, 100, -1, -10)
df_goal <- data.frame(ID, Type, Value)
I thought aboubt pivoting into wide format and then use dplyr::coalesce, but then I would loose the Type-column. I am pretty sure there is a solution using dplyr::group_by and dplyr::summarise. Initially I also thought about using dplyr::if_else in a dplyr::filter statement, but this is not possible I think.
Any help is appreciated!