找到每组的最小值
Find min for each group
我试图让 FL 在最短开始时间粘贴到每个组的 StartTimeFL 中。我使用以下代码找到了最短开始时间:
DF$StartTime<-with(DF, ave(DateTime, AlertID, FUN=min))
我拥有的数据框示例是:
AlertID DateTime FL StartTime StartTimeFL
FI 2017-06-07 23:00:45 300 2017-06-07 23:00:45
FI 2017-06-07 23:00:49 400 2017-06-07 23:00:45
FI 2017-06-07 23:00:53 300 2017-06-07 23:00:45
DJ 2017-05-07 03:00:00 500 2017-05-07 03:00:00
DJ 2017-05-07 03:00:04 400 2017-05-07 03:00:00
我想要的最终数据框如下所示:
AlertID DateTime FL StartTime StartTimeFL
FI 2017-06-07 23:00:45 300 2017-06-07 23:00:45 300
FI 2017-06-07 23:00:49 400 2017-06-07 23:00:45 300
FI 2017-06-07 23:00:53 300 2017-06-07 23:00:45 300
DJ 2017-05-07 03:00:00 500 2017-05-07 03:00:00 500
DJ 2017-05-07 03:00:04 400 2017-05-07 03:00:00 500
目前我使用的代码(如下所示)只将 StartTimeFL 放在每组开始时间所在的行中。
DF$StartTimeFL<-with(DF, QNHCorrectedAlt[ifelse(DateTime==StartTime, TRUE,NA)])
您可以使用 dplyr
执行这两个步骤:
library(dplyr);
df %>%
group_by(AlertID) %>%
mutate(StartTime = min(as.POSIXct(DateTime)), StartTimeFL = FL[which.min(StartTime)])
## A tibble: 5 x 5
## Groups: AlertID [2]
# AlertID DateTime FL StartTime StartTimeFL
# <fct> <fct> <int> <dttm> <dbl>
#1 FI 2017-06-07 23:00:45 300 2017-06-07 23:00:45 300
#2 FI 2017-06-07 23:00:49 400 2017-06-07 23:00:45 300
#3 FI 2017-06-07 23:00:53 300 2017-06-07 23:00:45 300
#4 DJ 2017-05-07 03:00:00 500 2017-05-07 03:00:00 500
#5 DJ 2017-05-07 03:00:04 400 2017-05-07 03:00:00 500
示例数据
df <- read.table(text = "AlertID DateTime FL
FI '2017-06-07 23:00:45' 300
FI '2017-06-07 23:00:49' 400
FI '2017-06-07 23:00:53' 300
DJ '2017-05-07 03:00:00' 500
DJ '2017-05-07 03:00:04' 400", header = T)
这是一个 data.table
的解决方案
library("data.table")
DF <- fread(
"AlertID DateTime FL
FI 2017-06-07_23:00:45 300
FI 2017-06-07_23:00:49 400
FI 2017-06-07_23:00:53 300
DJ 2017-05-07_03:00:00 500
DJ 2017-05-07_03:00:04 400")
DF[, StartTime:=min(DateTime), AlertID]
DF[, StartFL:=FL[DateTime==StartTime], AlertID][]
# > DF[, StartFL:=FL[DateTime==StartTime], AlertID][]
# AlertID DateTime FL StartTime StartFL
# 1: FI 2017-06-07_23:00:45 300 2017-06-07_23:00:45 300
# 2: FI 2017-06-07_23:00:49 400 2017-06-07_23:00:45 300
# 3: FI 2017-06-07_23:00:53 300 2017-06-07_23:00:45 300
# 4: DJ 2017-05-07_03:00:00 500 2017-05-07_03:00:00 500
# 5: DJ 2017-05-07_03:00:04 400 2017-05-07_03:00:00 500
对于现有的数据框DF
你可以这样做:
library("data.table")
setDT(DF)
DF[, StartTime:=min(DateTime), AlertID]
DF[, StartFL:=FL[DateTime==StartTime], AlertID]
DF[]
我找到了解决方案的答案,所以我想分享一下。我使用了我在问题中发布的两个公式,然后继续创建一个数据框,其中包含每个 AlertID 的一行及其开始时间和相应的 FL。
UniqueIDFL<-data.frame(DF)
UniqueIDFL<-UniqueIDFL[UniqueIDFL$DateTime==UniqueIDFL$StartTime,]
然后我继续根据 AlertID 和 Start Time 将这两个数据框重新合并在一起。
DF<-merge(DF, UniqueIDFL, by=c("AlertID", "StartTime"),all.x=TRUE)
可能不是最优雅的解决方案,但它完成了工作!
我试图让 FL 在最短开始时间粘贴到每个组的 StartTimeFL 中。我使用以下代码找到了最短开始时间:
DF$StartTime<-with(DF, ave(DateTime, AlertID, FUN=min))
我拥有的数据框示例是:
AlertID DateTime FL StartTime StartTimeFL
FI 2017-06-07 23:00:45 300 2017-06-07 23:00:45
FI 2017-06-07 23:00:49 400 2017-06-07 23:00:45
FI 2017-06-07 23:00:53 300 2017-06-07 23:00:45
DJ 2017-05-07 03:00:00 500 2017-05-07 03:00:00
DJ 2017-05-07 03:00:04 400 2017-05-07 03:00:00
我想要的最终数据框如下所示:
AlertID DateTime FL StartTime StartTimeFL
FI 2017-06-07 23:00:45 300 2017-06-07 23:00:45 300
FI 2017-06-07 23:00:49 400 2017-06-07 23:00:45 300
FI 2017-06-07 23:00:53 300 2017-06-07 23:00:45 300
DJ 2017-05-07 03:00:00 500 2017-05-07 03:00:00 500
DJ 2017-05-07 03:00:04 400 2017-05-07 03:00:00 500
目前我使用的代码(如下所示)只将 StartTimeFL 放在每组开始时间所在的行中。
DF$StartTimeFL<-with(DF, QNHCorrectedAlt[ifelse(DateTime==StartTime, TRUE,NA)])
您可以使用 dplyr
执行这两个步骤:
library(dplyr);
df %>%
group_by(AlertID) %>%
mutate(StartTime = min(as.POSIXct(DateTime)), StartTimeFL = FL[which.min(StartTime)])
## A tibble: 5 x 5
## Groups: AlertID [2]
# AlertID DateTime FL StartTime StartTimeFL
# <fct> <fct> <int> <dttm> <dbl>
#1 FI 2017-06-07 23:00:45 300 2017-06-07 23:00:45 300
#2 FI 2017-06-07 23:00:49 400 2017-06-07 23:00:45 300
#3 FI 2017-06-07 23:00:53 300 2017-06-07 23:00:45 300
#4 DJ 2017-05-07 03:00:00 500 2017-05-07 03:00:00 500
#5 DJ 2017-05-07 03:00:04 400 2017-05-07 03:00:00 500
示例数据
df <- read.table(text = "AlertID DateTime FL
FI '2017-06-07 23:00:45' 300
FI '2017-06-07 23:00:49' 400
FI '2017-06-07 23:00:53' 300
DJ '2017-05-07 03:00:00' 500
DJ '2017-05-07 03:00:04' 400", header = T)
这是一个 data.table
library("data.table")
DF <- fread(
"AlertID DateTime FL
FI 2017-06-07_23:00:45 300
FI 2017-06-07_23:00:49 400
FI 2017-06-07_23:00:53 300
DJ 2017-05-07_03:00:00 500
DJ 2017-05-07_03:00:04 400")
DF[, StartTime:=min(DateTime), AlertID]
DF[, StartFL:=FL[DateTime==StartTime], AlertID][]
# > DF[, StartFL:=FL[DateTime==StartTime], AlertID][]
# AlertID DateTime FL StartTime StartFL
# 1: FI 2017-06-07_23:00:45 300 2017-06-07_23:00:45 300
# 2: FI 2017-06-07_23:00:49 400 2017-06-07_23:00:45 300
# 3: FI 2017-06-07_23:00:53 300 2017-06-07_23:00:45 300
# 4: DJ 2017-05-07_03:00:00 500 2017-05-07_03:00:00 500
# 5: DJ 2017-05-07_03:00:04 400 2017-05-07_03:00:00 500
对于现有的数据框DF
你可以这样做:
library("data.table")
setDT(DF)
DF[, StartTime:=min(DateTime), AlertID]
DF[, StartFL:=FL[DateTime==StartTime], AlertID]
DF[]
我找到了解决方案的答案,所以我想分享一下。我使用了我在问题中发布的两个公式,然后继续创建一个数据框,其中包含每个 AlertID 的一行及其开始时间和相应的 FL。
UniqueIDFL<-data.frame(DF)
UniqueIDFL<-UniqueIDFL[UniqueIDFL$DateTime==UniqueIDFL$StartTime,]
然后我继续根据 AlertID 和 Start Time 将这两个数据框重新合并在一起。
DF<-merge(DF, UniqueIDFL, by=c("AlertID", "StartTime"),all.x=TRUE)
可能不是最优雅的解决方案,但它完成了工作!