I have a df with strings and numbers. I need to filter the max value by group excluding the year (date type).
I did this:
test
type Process RegionName Time Level a b c d e f g h i j k l m n o p q r s t u v w
a1 XYZ_1 ABC 2010 fixed 0 0 0 0 0 0 1 957 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
a1 XYZ_2 ABC 2010 fixed 0 0 0 0 0 0 0 61 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0
a1 XYZ_3 ABC 2010 fixed 0 0 0 0 0 0 0 695 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0
a1 XYZ_4 ABC 2010 fixed 0 0 0 0 0 0 0 525 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0
b1 XYZ_5 ABC 2010 fixed 0 0 0 0 0 0 10551 1168053 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
b1 XYZ_6 ABC 2010 fixed 0 0 0 0 0 0 0 7571 0 0 0 30 0 0 0 0 0 0 0 0 0 0 0
b1 XYZ_7 ABC 2010 fixed 0 0 0 0 0 0 0 10883 0 0 0 0 0 51 0 0 0 0 0 0 0 0 0
b1 XYZ_8 ABC 2010 fixed 0 0 0 0 0 0 0 40453 0 0 0 0 0 0 0 196 0 0 0 0 0 0 0
b1 XYZ_9 ABC 2010 fixed 0 0 0 0 0 0 0 24464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 53
c1 XYZ_10 ABC 2010 fixed 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0
c1 XYZ_11 ABC 2010 fixed 0 0 0 0 0 0 0 129 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
d1 XYZ_12 ABC 2010 fixed 0 0 0 0 0 0 2 1616 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
d1 XYZ_13 ABC 2010 fixed 0 0 0 0 0 0 0 762 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0
d1 XYZ_14 ABC 2010 fixed 0 0 0 0 0 0 0 1002 0 0 0 12 0 0 0 0 0 0 0 0 0 0 0
d1 XYZ_15 ABC 2010 fixed 0 0 0 0 0 0 0 556 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0
d1 XYZ_16 ABC 2010 fixed 0 0 0 0 0 0 0 961647 0 0 0 0 0 0 0 4661 0 0 0 0 0 0 0
d1 XYZ_17 ABC 2010 fixed 0 0 0 0 0 0 0 1381 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3
max_test <- test %>%
group_by(type) %>%
slice(which.max(a:w))
max_test
type Process RegionName Time Level a b c d e f g h i j k
a1 XYZ_1 ABC 2010 fixed 0 0 0 0 0 0 1 957 0 0 0
b1 XYZ_5 ABC 2010 fixed 0 0 0 0 0 0 10551 1168053 0 0 0
c1 XYZ_10 ABC 2010 fixed 0 0 0 0 0 0 0 0 0 0 0
d1 XYZ_12 ABC 2010 fixed 0 0 0 0 0 0 2 1616 0 0 0
a1 and b1 are what I expect. However, c1 and d1 are not.
For c1, I expect XYZ_11 because 129 > 16
For d1, I expect XYZ_16 because 961647 > 1616
Any idea what I am doing wrong?
Note: I have not introduce in the code to avoid Time values. I only do which.max(a:w). So, a1 might be considering 2010 instead of 957 as the highest value?