gpt4 book ai didi

r - 仅时间直方图(不计算日期)?

转载 作者:行者123 更新时间:2023-12-03 07:55:53 24 4
gpt4 key购买 nike

我在特定时间点有 181 个观察结果。时间是格式为“yyyy-mm-dd hh:mm:ss”的字符变量。

我通过以下方式将它们读入数据帧 DF<-read.csv("myCSV.csv")

我设法仅使用日期字符串“yyyy-mm-dd”创建日期的直方图(但它没有考虑时间......)。

我还需要做一个时间的直方图。不幸的是,我只是不知道为什么它不起作用。

mydate <- as.Date(DF$DateTime,format = "%Y-%m-%d %H:%M:%S")

将剥离时间。做的时候

mytime <- substr(as.character(DF$DateTime),12,16)

输入 hist() 时出现错误。

我想查看一天 24 小时内的分布情况。

dput(head(ExampleDF))
structure(list(Date = c("2019-05-26", "2019-05-27", "2019-09-19",
"2019-09-20", "2019-12-25", "2019-12-25"), Time = c("11:11:00",
"04:00:00", "07:00:00", "11:11:00", "04:00:00", "11:00:00"),
DateTime = c("2019-05-26 11:11:00", "2019-05-27 04:00:00",
"2019-09-19 07:00:00", "2019-09-20 11:11:00", "2019-12-25 04:00:00",
"2019-12-25 11:00:00"), TimeOnly = c("1905-06-21 11:11:00",
"1905-06-21 04:00:00", "1905-06-21 07:00:00", "1905-06-21 11:11:00",
"1905-06-21 04:00:00", "1905-06-21 11:00:00"), Intensity = c(5L,
10L, 10L, 10L, 7L, 10L), Comments = c("Feeling ill for days",
"", "", "", "", ""), SeriesComments = c("This section covers some ",
"", "", "", "", "")), row.names = c(NA, 6L), class = "data.frame")

最佳答案

您可以使用 lubridate::as_datetime()DF$DateTime 转换为日期时间,然后使用 lubridate::制作小时直方图小时():

DF$DateTime <- lubridate::as_datetime(DF$DateTime)

hist(lubridate::hour(DF$DateTime))

enter image description here

但是对于这些数据,您可能需要使用条形图而不是直方图,这很简单:

barplot(table(lubridate::hour(DF$DateTime)))

enter image description here

为了彻底起见,您还可以使用 lubridate::day() 并绘制它们:

par(mfrow = c(1,2))
barplot(table(lubridate::day(DF$DateTime)), xlab = "Day of Month", col = "darkred")
barplot(table(lubridate::hour(DF$DateTime)), xlab = "Hour of Day", col = "gold")

enter image description here

数据

DF <- data.frame(DateTime = as.character(sample(seq(from = as.POSIXct("2023-04-10 00:00:00", tz = "UTC"), 
to = as.POSIXct("2023-04-30 00:00:00", tz = "UTC"),
by = "hour"), 181)))

关于r - 仅时间直方图(不计算日期)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76067928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com