-1

I' m rather new to programming and this community. Hopefully can one of you help me, because i'm a stuck.

I have a df that looks as follows:

Time | Location | day | direction | speed

08:00| 143.3    | Mo  |    L      | 120.0

08:00| 143.3    | Mo  |    L      | 110.0

09:00| 143.3    | Mo  |    L      | 120.0

09:00| 143.3    | Mo  |    L      | 100.0

09:00| 143.3    | Mo  |    L      | 110.0

09:00| 143.3    | Mo  |    R      | 121.2

I want to merge the rows IF the Time and Location and day and direction are the same as another row. If this is all TRUE: it should merge and take the mean of the speed into a single row.

So it should be looking like this:

Time | Location | day | direction | speed

08:00| 143.3    | Mo  |    L      | 115.0

09:00| 143.3    | Mo  |    L      | 110.0

09:00| 143.3    | Mo  |    R      | 121.2

Can anyone help me with this?

ps. Sorry for the poor display of the df! I will edit this.

989
  • 12,579
  • 5
  • 31
  • 53
Arnand
  • 71
  • 11

1 Answers1

0
library(plyr)
result = plyr::ddply(df,c("Time","Location","day","direction"),function(x){
  data.frame(speed=mean(x$speed))
})
result
Nicholas Hamilton
  • 10,044
  • 6
  • 57
  • 88