如何将时间数据分组为 R 中的小时部分
How to Group Time data into hour sections in R
我有一个包含时间和心率数据的数据集。时间间隔目前相隔约 5 秒,每个时间间隔都有听力数据(即 7:21AM = 时间,97 = 值... 7:21:05 = 时间,102 = 值。)我想将将时间转换成小时,这样我就可以创建一个折线图来显示心率每小时的变化情况。当我尝试加载包含这么多数据点的数据时,图表无法正确加载。我将如何着手创建一个清晰的图表来显示心率如何从 0000 到 0100 到 0200 到 0300..等等?
更新 -- 好的,谢谢你的建议,我已经添加了我正在使用的可视化。本质上,我只想在代表早上 7 点到晚上 8 点的 x 轴上添加值。
structure(list(Id = c("user_1", "user_1", "user_1", "user_1",
"user_1", "user_1", "user_1", "user_1", "user_1", "user_1"),
Time = c("4/12/2016 7:21:00 AM", "4/12/2016 7:21:05 AM",
"4/12/2016 7:21:10 AM", "4/12/2016 7:21:20 AM", "4/12/2016 7:21:25 AM",
"4/12/2016 7:22:05 AM", "4/12/2016 7:22:10 AM", "4/12/2016 7:22:15 AM",
"4/12/2016 7:22:20 AM", "4/12/2016 7:22:25 AM"), Value = c(97L,
102L, 105L, 103L, 101L, 95L, 91L, 93L, 94L, 93L)), row.names = c(NA,
10L), class = "data.frame")
已更新
我们可以转换成POSIXct
格式,然后绘制数据。我们可以使用 scale_x_datetime
指定以 1 小时为间隔的绘图,仅显示小时、分钟和 AM/PM。
library(tidyverse)
library(lubridate)
df %>%
mutate(Time = as.POSIXct(strptime(Time, "%m/%d/%Y %I:%M:%S %p"), format = "%m/%d/%Y %H:%M:%OS %p")) %>%
ggplot(aes(x = Time, y = Value)) +
geom_line() +
theme_bw() +
scale_x_datetime(breaks = "1 hour", date_labels = "%I:%M %p")
输出
数据
df <- structure(list(Id = c("user_1", "user_1", "user_1", "user_1",
"user_1", "user_1", "user_1", "user_1", "user_1", "user_1", "user_1",
"user_1", "user_1"), Time = c("4/12/2016 6:50:00 AM", "4/12/2016 7:21:00 AM",
"4/12/2016 7:21:05 AM", "4/12/2016 7:21:10 AM", "4/12/2016 7:21:20 AM",
"4/12/2016 7:21:25 AM", "4/12/2016 7:22:05 AM", "4/12/2016 7:22:10 AM",
"4/12/2016 7:22:15 AM", "4/12/2016 7:22:20 AM", "4/12/2016 7:22:25 AM",
"4/12/2016 7:55:20 AM", "4/12/2016 8:05:25 AM"), Value = c(105L,
97L, 102L, 105L, 103L, 101L, 95L, 91L, 93L, 94L, 93L, 96L, 98L
)), row.names = c(NA, 13L), class = "data.frame")
原答案
如果要对每个小时进行汇总,那么我们可以将时间转换为小时,然后取该小时的平均值,然后转换回时间格式进行绘图。
library(tidyverse)
library(lubridate)
df %>%
mutate(Time = hour(hms(format(strptime(Time, "%I:%M:%S %p"), "%H:%M:%S")))) %>%
group_by(Time) %>%
summarise(Value = mean(Value)) %>%
mutate(Time = paste0(Time, ":00"),
Time = as_datetime(hm(Time))) %>%
ggplot(aes(x = Time, y = Value)) +
geom_line() +
theme_bw() +
scale_x_datetime(breaks = "1 hour", date_labels = "%H:%M %p")
输出
数据
set.seed(200)
time.seq = format(seq(from=as.POSIXct("04:00:00", format="%H:%M:%OS",tz="UTC"),
to=as.POSIXct("08:59:59", format="%H:%M:%OS", tz="UTC"), by = 5), "%I:%M:%S%p")
df <- data.frame(Time = time.seq, Value = round(runif(3600, 50, 150), digits = 0))
我有一个包含时间和心率数据的数据集。时间间隔目前相隔约 5 秒,每个时间间隔都有听力数据(即 7:21AM = 时间,97 = 值... 7:21:05 = 时间,102 = 值。)我想将将时间转换成小时,这样我就可以创建一个折线图来显示心率每小时的变化情况。当我尝试加载包含这么多数据点的数据时,图表无法正确加载。我将如何着手创建一个清晰的图表来显示心率如何从 0000 到 0100 到 0200 到 0300..等等?
更新 -- 好的,谢谢你的建议,我已经添加了我正在使用的可视化。本质上,我只想在代表早上 7 点到晚上 8 点的 x 轴上添加值。
structure(list(Id = c("user_1", "user_1", "user_1", "user_1",
"user_1", "user_1", "user_1", "user_1", "user_1", "user_1"),
Time = c("4/12/2016 7:21:00 AM", "4/12/2016 7:21:05 AM",
"4/12/2016 7:21:10 AM", "4/12/2016 7:21:20 AM", "4/12/2016 7:21:25 AM",
"4/12/2016 7:22:05 AM", "4/12/2016 7:22:10 AM", "4/12/2016 7:22:15 AM",
"4/12/2016 7:22:20 AM", "4/12/2016 7:22:25 AM"), Value = c(97L,
102L, 105L, 103L, 101L, 95L, 91L, 93L, 94L, 93L)), row.names = c(NA,
10L), class = "data.frame")
已更新
我们可以转换成POSIXct
格式,然后绘制数据。我们可以使用 scale_x_datetime
指定以 1 小时为间隔的绘图,仅显示小时、分钟和 AM/PM。
library(tidyverse)
library(lubridate)
df %>%
mutate(Time = as.POSIXct(strptime(Time, "%m/%d/%Y %I:%M:%S %p"), format = "%m/%d/%Y %H:%M:%OS %p")) %>%
ggplot(aes(x = Time, y = Value)) +
geom_line() +
theme_bw() +
scale_x_datetime(breaks = "1 hour", date_labels = "%I:%M %p")
输出
数据
df <- structure(list(Id = c("user_1", "user_1", "user_1", "user_1",
"user_1", "user_1", "user_1", "user_1", "user_1", "user_1", "user_1",
"user_1", "user_1"), Time = c("4/12/2016 6:50:00 AM", "4/12/2016 7:21:00 AM",
"4/12/2016 7:21:05 AM", "4/12/2016 7:21:10 AM", "4/12/2016 7:21:20 AM",
"4/12/2016 7:21:25 AM", "4/12/2016 7:22:05 AM", "4/12/2016 7:22:10 AM",
"4/12/2016 7:22:15 AM", "4/12/2016 7:22:20 AM", "4/12/2016 7:22:25 AM",
"4/12/2016 7:55:20 AM", "4/12/2016 8:05:25 AM"), Value = c(105L,
97L, 102L, 105L, 103L, 101L, 95L, 91L, 93L, 94L, 93L, 96L, 98L
)), row.names = c(NA, 13L), class = "data.frame")
原答案
如果要对每个小时进行汇总,那么我们可以将时间转换为小时,然后取该小时的平均值,然后转换回时间格式进行绘图。
library(tidyverse)
library(lubridate)
df %>%
mutate(Time = hour(hms(format(strptime(Time, "%I:%M:%S %p"), "%H:%M:%S")))) %>%
group_by(Time) %>%
summarise(Value = mean(Value)) %>%
mutate(Time = paste0(Time, ":00"),
Time = as_datetime(hm(Time))) %>%
ggplot(aes(x = Time, y = Value)) +
geom_line() +
theme_bw() +
scale_x_datetime(breaks = "1 hour", date_labels = "%H:%M %p")
输出
数据
set.seed(200)
time.seq = format(seq(from=as.POSIXct("04:00:00", format="%H:%M:%OS",tz="UTC"),
to=as.POSIXct("08:59:59", format="%H:%M:%OS", tz="UTC"), by = 5), "%I:%M:%S%p")
df <- data.frame(Time = time.seq, Value = round(runif(3600, 50, 150), digits = 0))