r - dplyr>Get rows with minimum and maximum of variable -
i have data.frame want return min
, max
time observations of value.
df<- data.frame( time=c(24594.55, 29495.45, 24594.55, 39297.27, 24594.55, 34396.36, 19693.64, 14792.73, 29495.45), mz=c(-0.04729751, -0.50902297, -0.04376393, -0.22218980, -0.36407263, -0.38341534, -0.34597255, -0.01480776, -0.00999671), set_nbr=c(1, 1,1, 2, 2, 2, 3, 3, 3)) library(dplyr) min_time <- df %>% group_by(set_nbr) %>% slice(which(mz<0))%>% filter(rank(time,ties.method="min")==1)%>% distinct min_time ##source: local data frame [3 x 3] ##groups: set_nbr time mz set_nbr ## 1 24594.55 -0.04729751 1 ## 2 24594.55 -0.36407263 2 ## 3 14792.73 -0.01480776 3
this works, when try max_time, strange result comes:
max_time <- df %>% group_by(set_nbr) %>% slice(which(mz<0))%>% filter(rank(time,ties.method="max")==1)%>% distinct max_time ##source: local data frame [2 x 3] ##groups: set_nbr time mz set_nbr ##1 24594.55 -0.36407263 2 ##2 14792.73 -0.01480776 3
set_nbr
1, , max
time
values incorrect. don't know why.
expected output
max_time time mz set_nbr ##1 29495.45 -0.50902297 1 ##2 39297.27 -0.22218980 2 ##3 29495.45 -0.00999671 3
try
df %>% group_by(set_nbr) %>% filter(time==max(time)) # time mz set_nbr #1 29495.45 -0.50902297 1 #2 39297.27 -0.22218980 2 #3 29495.45 -0.00999671 3
or
df %>% group_by(set_nbr) %>% slice(which.max(time)) # time mz set_nbr #1 29495.45 -0.50902297 1 #2 39297.27 -0.22218980 2 #3 29495.45 -0.00999671 3
regarding why code didn't work
df %>% group_by(set_nbr) %>% slice(which(mz <0)) %>% mutate(rn = rank(time, ties.method='max')) # time mz set_nbr rn #1 24594.55 -0.04729751 1 2 #2 29495.45 -0.50902297 1 3 #3 24594.55 -0.04376393 1 2 #4 39297.27 -0.22218980 2 3 #5 24594.55 -0.36407263 2 1 #6 34396.36 -0.38341534 2 2 #7 19693.64 -0.34597255 3 2 #8 14792.73 -0.01480776 3 1 #9 29495.45 -0.00999671 3 3
if @ output, 'set_nbr' group '1', there no '1' 'rn' there ties. do
df %>% group_by(set_nbr) %>% slice(which(mz <0)) %>% filter(rn = rank(-time, ties.method='first')==1) # time mz set_nbr #1 29495.45 -0.50902297 1 #2 39297.27 -0.22218980 2 #3 29495.45 -0.00999671 3
Comments
Post a Comment