R 如何将时间从字符转换为时间并按小时分组
需要将字符数据转换为实际时间,并按小时分组R 如何将时间从字符转换为时间并按小时分组,r,R,需要将字符数据转换为实际时间,并按小时分组 library(dplyr) library(stringr) library(lubridate) library(chron) test <- read.csv("O:/Commercial Team/Customer Business Unit/ECOMMERCE and DIGITAL/9. TEAM/4_M_APPLEYARD.GEO.csv", header = TRUE) colnames(test) time <- str
library(dplyr)
library(stringr)
library(lubridate)
library(chron)
test <- read.csv("O:/Commercial Team/Customer Business Unit/ECOMMERCE and DIGITAL/9. TEAM/4_M_APPLEYARD.GEO.csv", header = TRUE)
colnames(test)
time <- str_split(test$ProcessedDate, " ", simplify = TRUE)
time2 <- c(time[,2])
库(dplyr)
图书馆(stringr)
图书馆(lubridate)
图书馆(计时)
测试签出strtime
。我不知道您的数据是什么样子的,因此下面是一个示例:
z <- strptime('2019-06-4 09:32:14', format = '%Y-%m-%d %H:%M%:S')
z您可以创建一个小时列,并使用groupby()
:
库(tidyverse)
图书馆(lubridate)
df%
分组依据(处理时间)%>%
总结(总和=总和(占位符值))
打印(df2,n=Inf)
ggplot(df2,aes(x=平均系数(处理小时),y=总和))+
geom_col()
基本上是拉索夫利森答案的一个简单版本,因为我认为你可以用lubridate做任何事情(但如果没有你的数据样本,很难说):
库(tidyverse)
图书馆(lubridate)
#为此答案创建虚拟数据集
#如果您已有数据,请跳过此步骤
df您到底在哪里被卡住了?输出看起来更好更简单,但我得到了一个错误:警告消息:所有格式都无法解析。没有找到格式。啊,听起来您可能需要修改ymd\u hm()
部分。如果您的时间戳是美国日期格式“06/20/2019 11:20”,则尝试使用mdy_hm
作为润滑功能。或者,lubridate足够聪明,可以跳过POSIX转换,有时可以从角色版本中提取小时数。如果是“04/06/2019 11:20”,请尝试dmy\u hm()
,如果戳记包含秒数“04/06/2019 11:20:50”,请尝试dmy\u hms()
df trytest$dReceivedDatePOSIX这不起作用,因为无论我使用ProcessedDate还是ReceivedDate(这是两个不同的日期列),图形都保持不变。我不遵循这一点,但我相信共享一些输入数据会有所帮助,即日期列。我刚刚在列ProcessedDate
中创建了随机日期时间值,但我不知道日期时间变量的格式。它是一个csv文件,格式为文本或R字符。由于保密原因,我无法共享输入数据,我的意思是我希望看到时间格式,因为它在.csv文件中。时间在文件中的确切表示方式是什么?
library(tidyverse)
library(lubridate)
df <- tibble(ProcessedDate = sample(seq.POSIXt(from = as_datetime("2018-01-01 00:00:00"),
to = as_datetime("2018-12-31 23:59:59"),
by = "sec"), size = 1000, replace = TRUE),
placeholder_value = rlnorm(n = 1000))
df2 <- df %>%
mutate(Processed_hour = hour(ProcessedDate),
Processed_minute = minute(ProcessedDate),
Processed_second = second(ProcessedDate)) %>%
group_by(Processed_hour) %>%
summarise(sum = sum(placeholder_value))
print(df2, n = Inf)
ggplot(df2, aes(x = as_factor(Processed_hour), y = sum)) +
geom_col()
library(tidyverse)
library(lubridate)
#create imaginary data set for this answer
#skip this step if you already have your data
df <- tibble(yourtimestamp = c("2019-06-04 11:20", "2019-06-04 11:25", "2019-06-04 12:00"))
#convert your timestamp vector to POSIX
df$yourtimestamp <- ymd_hm(df$yourtimestamp)
#the function you use here depends on what order the parts of your time stamp are,
#so for example if your character timestamp is US style: month, day, year
#such as
#06/20/2019 11:20 then use mdy_hm() instead
#or if it was UK style 20/06/2019 11:20 then use dmy_hm() etc
#or if it contains seconds such as 06/20/2019 11:20:47 use mdy_hms()
#get hour
df$just_the_hour <- hour(df$yourtimestamp)
#group by hour and summarise
by_hour <- group_by(df, just_the_hour)
s <- summarise(by_hour, num_events = n())
s
# A tibble: 2 x 2
just_the_hour num_events
<int> <int>
1 11 2
2 12 1