Phenomenal One
Phenomenal One

Reputation: 2587

Getting Max value from dataset with redundant rows in R

I have a data set with 3 ids and a cost. I need to get maximum of this cost if 3 ids repeat in the set ex:

id1 id2 id3  cost
1   100 450  1000
2   678 098  786
3   897 867  7897
1   100 450  1500

my output should be

id1 id2 id3  cost
2   678 098  786
3   897 867  7897
1   100 450  1500

because all 3 ids are the same as 1 and max(1500,1000) = 1000

Also, how to obtain minimum of the same set.

Upvotes: 1

Views: 54

Answers (1)

akrun
akrun

Reputation: 887088

We can use summarise_each

library(dplyr)
df1 %>%
   group_by(id1, id2, id3) %>%
   summarise_each(funs(max))

Or using data.table

library(data.table)
setDT(df1)[, lapply(.SD, max) , by = .(id1, id2, id3)]

Or using aggregate from base R

aggregate(cost~., df1, FUN = max)

Upvotes: 1

Related Questions