Reputation: 7
Is there a way to calculate means and standard error for time duration data? I have tried to work through similar fixes on this site but have not been successful.
The data needs to be grouped by "Site" and "Treatment".
I'd like to be able to calculate the mean and standard error of "HH.MM.SS' for each "Treatment" according to each "Site".
names(Socials)
[1] "Site" "Date" "Treatment" "Block" "HH.MM.SS" "Max.N"
A sample of the data is below (one of two sites):
Site Date Treatment Block HH.MM.SS Max.N
MH 2018-10-06 C 1 2020-07-05 00:08:05 9
MH 2018-10-08 D 1 2020-07-05 00:12:56 6
MH 2018-10-07 V 1 2020-07-05 00:57:52 4
MH 2018-10-05 V + D 1 2020-07-05 00:53:14 4
MH 2018-10-12 C 2 2020-07-05 00:03:57 7
MH 2018-10-10 D 2 2020-07-05 00:21:34 2
MH 2018-10-11 V 2 2020-07-05 01:10:24 4
MH 2018-10-09 V + D 2 2020-07-05 01:15:17 6
MH 2018-10-15 C 3 2020-07-05 00:08:44 1
MH 2018-10-13 D 3 2020-07-05 00:58:22 23
MH 2018-10-14 V 3 2020-07-05 00:00:07 3
MH 2018-10-16 V + D 3 2020-07-05 01:09:12 4
# CreatePOSIXct time objects for date.
Socials$Date <- as.POSIXct(Socials$Date, format = "%d/%m/%y")
# CreatePOSIXct time objects for the total time duration for each treatment
Socials$HH.MM.SS <- as.POSIXct(Socials$HH.MM.SS, format = "%H:%M:%OS")
I have not provided all the data here, so the data could be subset, as per the below, to produce a working example
#Subset Data - split sites MH and GI
MH.data <- subset(Socials, Site == 'MH')
GI.data <- subset(Socials, Site == 'GI')
##### Calculate summary statistics for Observations ######
library(data.table)
library(plotrix)
df1 = data.table(Socials)
df1_output = df1[, .("HH.MM.SS" = mean(HH.MM.SS),
"std" = std.error(Treatment)),
by = c("Site", "Treatment")]
Any assistance would be greatly appreciated.
Thanks in advance. I hope the information provided here is okay.
Upvotes: 0
Views: 120
Reputation: 328
I think you can just do it normally. This just worked for me:
> x=c(as.Date("2018-10-06"), as.Date("2018-11-06"), as.Date("2018-12-06"))
> mean(x)
[1] "2018-11-05"
> sd(x)
[1] 30.50137
> std.error(x)
[1] 17.60997
Upvotes: 0