1
 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?

2
  • as the error said, you have to transform the mtime column to character or to a time object. Use for example as.character(d$mtime) Commented May 30, 2016 at 15:14
  • not an answer because you asked for dplyr, but data.table solution would be setDT(kj); kj[, max(as.character(as.numeric(Speed))), by = mtime] Commented May 30, 2016 at 18:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.