计算超过 20 秒间隔的平均值并按另一列分组
Compute average over 20 second intervals and group by another column
我正在处理在象海豹潜水期间收集的不同变量的大型数据集。我想详细分析我的数据(20 秒间隔)。我想将我的数据分为 20 秒间隔,基本上我只想得到每 20 秒的平均值,这样我就可以 运行 对这些数据间隔进行更多分析。但是,我需要按潜水次数对我的数据进行分组,这样我就不会对来自不同潜水的信息进行分类。
到目前为止我尝试了三种方法:
period.apply()
但我无法使用此功能分组。
split()
通过 dive # 对我的数据进行子集化,但似乎无法找到一种方法来计算
这些子集中超过 20 秒间隔的不同列。
- openair 包,使用
timeaverage()
但继续报错(见下面的代码)。
下面是数据的样子,以及我试过的代码。我想要每 20 秒的深度、MSA、rate_s 和 HR 的方法 window - 按 diveNum 和 ~理想情况下~ 分组 D_phase.
> head(seal_dives)
datetime seal_ID Depth MSA D_phase diveNum rate_s HR
1 2018-04-06 14:47:51 Congaree 4.5 0.20154042 D 1 NA 115.3846
2 2018-04-06 14:47:51 Congaree 4.5 0.20154042 D 1 NA 117.6471
3 2018-04-06 14:47:52 Congaree 4.5 0.11496760 D 1 NA 115.3846
4 2018-04-06 14:47:52 Congaree 4.5 0.11496760 D 1 NA 122.4490
5 2018-04-06 14:47:53 Congaree 4.5 0.05935992 D 1 NA 113.2075
6 2018-04-06 14:47:53 Congaree 4.5 0.05935992 D 1 NA 113.2075
#openair package using timeaverage, results in error message
> library(openair)
> seal_20<-timeAverage(
seal_dives,
avg.time = "20 sec",
data.thresh = 0,
statistic = "mean",
type = c("diveNum","D_phase"),
percentile = NA,
start.date = NA,
end.date = NA,
vector.ws = FALSE,
fill = FALSE
)
Can't find the variable(s) date
Error in checkPrep(mydata, vars, type = "default", remove.calm = FALSE, :
#converting to time series and using period.apply(), but can't find a way to group them by dive #, or use split() then convert to time series.
#create a time series data class from our data frame
> seal_dives$datetime<-as.POSIXct(seal_dives$datetime,tz="GMT")
> seal_xts <- xts(seal_dives, order.by=seal_dives[,1])
> seal_20<-period.apply(seal_xts$Depth, endpoints(seal_xts$datetime, "seconds", 20), mean)
#split data by dive # but don't know how to do averages over 20 seconds
> seal_split<-split(seal_dives, seal_dives$diveNum)
也许有一种神奇的方法可以做到这一点,但我还没有在互联网上找到它,或者也许我只是在我的一种方法中做错了。
您可以使用 lubridate
中的 floor_date
函数每 20 秒对数据进行一次分箱。将它们与 diveNum
和 D_phase
一起分组,以使用 across
.
获得其他列的平均值
library(dplyr)
library(lubridate)
result <- df %>%
group_by(diveNum, D_phase, datetime = floor_date(datetime, '20 sec')) %>%
summarise(across(c(Depth, MSA, rate_s, HR), mean, na.rm = TRUE), .groups = 'drop')
result
我正在处理在象海豹潜水期间收集的不同变量的大型数据集。我想详细分析我的数据(20 秒间隔)。我想将我的数据分为 20 秒间隔,基本上我只想得到每 20 秒的平均值,这样我就可以 运行 对这些数据间隔进行更多分析。但是,我需要按潜水次数对我的数据进行分组,这样我就不会对来自不同潜水的信息进行分类。
到目前为止我尝试了三种方法:
period.apply()
但我无法使用此功能分组。split()
通过 dive # 对我的数据进行子集化,但似乎无法找到一种方法来计算 这些子集中超过 20 秒间隔的不同列。- openair 包,使用
timeaverage()
但继续报错(见下面的代码)。
下面是数据的样子,以及我试过的代码。我想要每 20 秒的深度、MSA、rate_s 和 HR 的方法 window - 按 diveNum 和 ~理想情况下~ 分组 D_phase.
> head(seal_dives)
datetime seal_ID Depth MSA D_phase diveNum rate_s HR
1 2018-04-06 14:47:51 Congaree 4.5 0.20154042 D 1 NA 115.3846
2 2018-04-06 14:47:51 Congaree 4.5 0.20154042 D 1 NA 117.6471
3 2018-04-06 14:47:52 Congaree 4.5 0.11496760 D 1 NA 115.3846
4 2018-04-06 14:47:52 Congaree 4.5 0.11496760 D 1 NA 122.4490
5 2018-04-06 14:47:53 Congaree 4.5 0.05935992 D 1 NA 113.2075
6 2018-04-06 14:47:53 Congaree 4.5 0.05935992 D 1 NA 113.2075
#openair package using timeaverage, results in error message
> library(openair)
> seal_20<-timeAverage(
seal_dives,
avg.time = "20 sec",
data.thresh = 0,
statistic = "mean",
type = c("diveNum","D_phase"),
percentile = NA,
start.date = NA,
end.date = NA,
vector.ws = FALSE,
fill = FALSE
)
Can't find the variable(s) date
Error in checkPrep(mydata, vars, type = "default", remove.calm = FALSE, :
#converting to time series and using period.apply(), but can't find a way to group them by dive #, or use split() then convert to time series.
#create a time series data class from our data frame
> seal_dives$datetime<-as.POSIXct(seal_dives$datetime,tz="GMT")
> seal_xts <- xts(seal_dives, order.by=seal_dives[,1])
> seal_20<-period.apply(seal_xts$Depth, endpoints(seal_xts$datetime, "seconds", 20), mean)
#split data by dive # but don't know how to do averages over 20 seconds
> seal_split<-split(seal_dives, seal_dives$diveNum)
也许有一种神奇的方法可以做到这一点,但我还没有在互联网上找到它,或者也许我只是在我的一种方法中做错了。
您可以使用 lubridate
中的 floor_date
函数每 20 秒对数据进行一次分箱。将它们与 diveNum
和 D_phase
一起分组,以使用 across
.
library(dplyr)
library(lubridate)
result <- df %>%
group_by(diveNum, D_phase, datetime = floor_date(datetime, '20 sec')) %>%
summarise(across(c(Depth, MSA, rate_s, HR), mean, na.rm = TRUE), .groups = 'drop')
result