Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - Subset data based on Minimum Value

This might an easy one. Here's the data:

dat <- read.table(header=TRUE, text="
Seg  ID  Distance
Seg46      V21 160.37672
Seg72      V85 191.24400
Seg373      V85 167.38930
Seg159     V147  14.74852
Seg233     V171 193.01636
Seg234     V171 200.21458

                   ")
dat
Seg  ID  Distance
Seg46      V21 160.37672
Seg72      V85 191.24400
Seg373      V85 167.38930
Seg159     V147  14.74852
Seg233     V171 193.01636
Seg234     V171 200.21458

I am intending to get a table like the following that will give me Seg for the minimized distance (as duplication is seen in ID.

Seg Crash_ID  Distance
Seg46      V21 160.37672
Seg373      V85 167.38930
Seg159     V147  14.74852
Seg233     V171 193.01636

I am trying to use ddply to solve it; but it is not reaching there.

ddply(dat, "Seg", summarize, min = min(Distance))
Seg       min
Seg159  14.74852
Seg233 193.01636
Seg234 200.21458
Seg373 167.38930
Seg46 160.37672
Seg72 191.24400
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can subset the rows with which.min. After grouping with 'ID', we slice the rows based on the position of minimum 'Distance'.

library(dplyr)
dat %>% 
   group_by(ID) %>% 
   slice(which.min(Distance))

A similar option using data.table would be

library(data.table)
setDT(dat)[, .SD[which.min(Distance)], by = ID]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...