我有几天的心率数据,每时每刻(随机缺失的数据间隙(,如下所示:
structure(list(TimePoint = structure(c(1523237795, 1523237796,
1523237797, 1523237798, 1523237799, 1523237800, 1523237801, 1523237802,
1523237803, 1523237804), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
HR = c(80L, 83L, 87L, 91L, 95L, 99L, 102L, 104L, 104L, 103L
)), row.names = c(NA, 10L), class = "data.frame")
------------------------------
TimePoint HR
1 2018-04-09 01:36:35 80
2 2018-04-09 01:36:36 83
3 2018-04-09 01:36:37 87
4 2018-04-09 01:36:38 91
5 2018-04-09 01:36:39 95
6 2018-04-09 01:36:40 99
7 2018-04-09 01:36:41 102
8 2018-04-09 01:36:42 104
9 2018-04-09 01:36:43 104
10 2018-04-09 01:36:44 103
.
.
.
我想将 Scale(center = T, scale = T( 函数应用于数据,以便在参与者之间标准化。
- 但是,我不想在一整天的可用数据中进行规范化,而是每 24 小时对一次数据进行规范化。
- 因此,如果参与者有 3 天的数据,HR 将分别缩放到 3 次 z 分布;每次都是各自的一天
我无法成功做到这一点。
# read csv
DF = read.csv(x)
# make sure date stamp is read YYYY Month Day & convert timestamp into class POSIXct
x2 = as.POSIXct(DF[,1], format = '%d.%m.%Y %H:%M:%S', tz = "UTC") %>% data.frame()
# rename column
colnames(x2)[1] = "TimePoint"
# add the participant HR data to this dataframe
x2$HR = DF[,2]
# break time stamps into 60 minute windows
by60 = cut(x2$TimePoint, breaks = "60 min")
# get the average HR per 60 min window
DF_Sum = aggregate(HR ~ by60, FUN=mean, data=x2)
# add weekday /hours for future plot visualization
DF_Sum$WeekDay = wday(DF_Sum$by60, label = T)
DF_Sum$Hour = hour(DF_Sum$by60)
我能够按时间序列拆分数据并按小时平均心率,但我似乎无法正确添加缩放功能。
感谢帮助。
为每个患者创建 24 小时的时间间隔,group_by
患者和时间间隔,然后计算每个组的缩放心率。
library(dplyr)
df %>%
#remove the following mutate and replace ID in group_by by the ID's column name in your data set
mutate(ID=1) %>%
group_by(ID, Int=cut(TimePoint, breaks="24 hours")) %>%
mutate(HR_sc=scale(HR, center = TRUE, scale = TRUE))
# A tibble: 10 x 5
# Groups: ID, Int [1]
TimePoint HR ID Int HR_sc
<dttm> <int> <dbl> <fct> <dbl>
1 2018-04-09 01:26:35 80 1 2018-04-09 01:00:00 -1.63
2 2018-04-09 01:28:16 83 1 2018-04-09 01:00:00 -1.30
3 2018-04-09 01:29:57 87 1 2018-04-09 01:00:00 -0.860
4 2018-04-09 01:31:38 91 1 2018-04-09 01:00:00 -0.419
5 2018-04-09 01:33:19 95 1 2018-04-09 01:00:00 0.0221
6 2018-04-09 01:33:20 99 1 2018-04-09 01:00:00 0.463
7 2018-04-09 01:35:01 102 1 2018-04-09 01:00:00 0.794
8 2018-04-09 01:36:42 104 1 2018-04-09 01:00:00 1.01
9 2018-04-09 01:38:23 104 1 2018-04-09 01:00:00 1.01
10 2018-04-09 01:39:59 103 1 2018-04-09 01:00:00 0.905