user1334824
user1334824

Reputation: 43

Removing a Duplicate in one column using the condition from another using Dplyr

 mtime          Speed
 04:27:00.000   83,0903472900391
 04:28:00.000   90,4319076538086
 04:28:00.000   83,3221054077148

I need to remove the duplicates on the mtime column in R by using the condition of the maximum value for speed. the resulting table should look like this.

 mtime          Speed
 04:27:00.000   83,0903472900391
 04:28:00.000   90,4319076538086

There can be more than one duplicate value for time but it should remove all the values except the one with the highest speed.

k<- filter(kj, unique(mtime), max(vPkwCommon))

when I tried this using dplyr, it returned "Error: ‘max’ not meaningful for factors"

Any suggestions?

Upvotes: 1

Views: 710

Answers (1)

dmontaner
dmontaner

Reputation: 2165

Order first your rows and then filter out duplicates in the mtime columns:

kj %>% arrange (mtime, - Speed) %>% filter (!duplicated (mtime))

The - sign will order decreasingly your Speed column. Then, when you use the !duplicated filter, just the first value in the duplicated run of mtime will be kept, and this will include the maximum value of Speed.

Upvotes: 1

Related Questions