Suppose the following data frame:
dfX <- data.frame('a' = c('A', 'A', 'B', 'B', 'B', 'C', 'C', 'D', 'D', 'D'),
'b' = c('c2', 'c2', 'c8', 'c8', 'c4', 'c7', 'c7', 'c9', 'c9','c9'),
'c' = c('f34', 'f34', 'm92', 'm92', 'm92', 'g22', 'g22', 'i41', 'i41', 'i41'),
'd' = c('Check', 'Check', 'Check', 'Check', 'UnCheck', 'Check', 'Check', 'Check', 'Check','Check'),
'val1' = c(54, '', 37, '', '', 51, '', 74, '', ''),
'val2' = c('', 59, '', 87, 84, '', 62, '', 27, 85))
dfX
a b c d val1 val2
1 A1 c2 f34 Check 54
2 A1 c2 f34 Check 59
3 A2 c8 m92 Check 37
4 A2 c8 m92 Check 87
5 A2 c4 m92 UnCheck 84
6 A3 c7 g22 Check 51
7 A3 c7 g22 Check 62
8 A4 c9 i41 Check 74
9 A4 c9 i41 Check 27
10 A4 c9 i41 Check 85
I would like to merge columns val1 and val2 grouping them by the equivalent values of columns a, b, c, BUT if column d says 'Uncheck', then the row should be erased from the resulting data frame.
On top of that, if there is more than one value in a column per group (like in case of rows A4), only the highest value of the repeated column should be kept
So far I was aiming with dplyr something like this: (This post as source)
FuncX <- function(x) x[x != '']
dfY <- dfX %>%
group_by(a,b,c) %>%
summarise_each(funs(FuncX))
But adding the d column and the said condition that if this column marks 'Unchecked', then it should be erased, I couldn't find a way around it. Also failed at having as output only the max() value of the A4 rows
The desired output should be like so:
dfY
a b c d val1 val2
1 A c2 f34 Check 54 59
2 B c8 m92 Check 37 87
3 C c7 g22 Check 51 62
4 D c9 i41 Check 74 85