0
我有一個data.frame,我想返回min
和max
時間觀察值 值。dplyr>獲取具有最小和最大變量的行
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
這個工作,但是當我試圖讓MAX_TIME,奇怪的結果來了:
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
值不正確。我不知道爲什麼。
預計輸出
max_time
time Mz set_nbr
##1 29495.45 -0.50902297 1
##2 39297.27 -0.22218980 2
##3 29495.45 -0.00999671 3
我不確定你在用'filter' +'rank'做什麼,但是你知道'arrange'函數嗎?像'df%>%排列(時間)%>%group_by(set_nbr)%>%slice(c(1,n()))'' – Frank