The dataframe df1
summarizes one-hour intervals for several people. I have in other dataframe df2
data about a specific variable for these people and for specific moments of the day.
As an example:
df1<- data.frame(Round_datetime=c("2016-08-23 11:00:00","2016-08-23 11:00:00","2016-08-23 12:00:00","2016-08-23 12:00:00"),
Person= c("Sophie","Anna","Sophie","Anna"))
df1$Round_datetime<-as.POSIXct(df1$Round_datetime, format="%Y-%m-%d %H",tz="UTC")
df1
Round_datetime Person
1 2016-08-23 11:00:00 Sophie
2 2016-08-23 11:00:00 Anna
3 2016-08-23 12:00:00 Sophie
4 2016-08-23 12:00:00 Anna
df2<- data.frame(DateTime=c("2016-08-23 10:29:08.324","2016-08-23 10:39:36.326","2016-08-23 10:44:08.724","2016-08-23 10:59:46.324","2016-08-23 11:19:22.324","2016-08-23 11:29:53.324","2016-08-23 11:34:14.324","2016-08-23 11:47:49.324","2016-08-23 11:54:58.324","2016-08-23 11:59:13.324","2016-08-23 12:12:34.324","2016-08-23 12:23:43.324","2016-08-23 12:32:14.324","2016-08-23 12:29:28.324"),
Person=c("Sophie","Anna","Sophie","Anna","Sophie","Anna","Sophie","Anna","Sophie","Anna","Sophie","Anna","Sophie","Anna"),
Value=c(10,15,5,10,20,15,10,5,25,15,10,5,10,20))
df2$DateTime<-as.POSIXct(df2$DateTime, format="%Y-%m-%d %H:%M:%OS",tz="UTC")
df2
DateTime Person Value
1 2016-08-23 10:29:08.323 Sophie 10
2 2016-08-23 10:39:36.325 Anna 15
3 2016-08-23 10:44:08.723 Sophie 5
4 2016-08-23 10:59:46.323 Anna 10
5 2016-08-23 11:19:22.323 Sophie 20
6 2016-08-23 11:29:53.323 Anna 15
7 2016-08-23 11:34:14.323 Sophie 10
8 2016-08-23 11:47:49.323 Anna 5
9 2016-08-23 11:54:58.323 Sophie 25
10 2016-08-23 11:59:13.323 Anna 15
11 2016-08-23 12:12:34.323 Sophie 10
12 2016-08-23 12:23:43.323 Anna 5
13 2016-08-23 12:32:14.323 Sophie 10
14 2016-08-23 12:29:28.323 Anna 20
I would like to add some statistics in the dataframe df1
based on the info in df2
. For instance, I would like to include the statistics mean
, standard deviation
and standard error
.
I've managed to calculate the mean
and the standard deviation
, but I don't know how to include with the function data.table
the standard error
.
Does anyone know how to do it? I show below the code I did so far:
setDT(df1)[, Round_datetime := ymd_hms(Round_datetime)]
setDT(df2)[, dt_floor := round_date(ymd_hms(DateTime), unit = "hour")]
df2[df1, .(mean = mean(Value),
sd = sd(Value)),
on = .(Person, dt_floor = Round_datetime), by = .EACHI]
Person dt_floor mean sd
1: Sophie 2016-08-23 11:00:00 12.50000 10.606602
2: Anna 2016-08-23 11:00:00 13.33333 2.886751
3: Sophie 2016-08-23 12:00:00 15.00000 8.660254
4: Anna 2016-08-23 12:00:00 11.25000 7.500000
I've found a solution.
Using the function std.error()
from the package plotrix
.
library(plotrix)
setDT(df1)[, Round_datetime := ymd_hms(Round_datetime)]
setDT(df2)[, dt_floor := round_date(ymd_hms(DateTime), unit = "hour")]
df2[df1, .(mean = mean(Value),
sd = sd(Value),
se = std.error(Value)),
on = .(Person, dt_floor = Round_datetime), by = .EACHI]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.