如何仅从R中的Datetime变量提取时间参数?

如何仅从R中的Datetime变量提取时间参数?,r,datetime,posixct,posixlt,R,Datetime,Posixct,Posixlt,在R数据帧中,我有时间变量。数据的格式为%a-%b-%d%H:%M:%S。例如 2015-03-23 20:00:00 我只想得到以下数据 20:00:00 我已根据上述变量创建了一个表格,并尝试制作一个折线图: Var1 Var2 Freq 1 2015-03-24 00:00:00 RT 612 2 2015-03-24 01:00:00 RT 65 3 2015-03-24 06:00:00

在R数据帧中,我有时间变量。数据的格式为%a-%b-%d%H:%M:%S。例如

2015-03-23 20:00:00
我只想得到以下数据

  20:00:00
我已根据上述变量创建了一个表格,并尝试制作一个折线图:

                     Var1 Var2  Freq
    1 2015-03-24 00:00:00   RT   612
    2 2015-03-24 01:00:00   RT    65
    3 2015-03-24 06:00:00   RT    58
    4 2015-03-24 07:00:00   RT  5132
    5 2015-03-24 08:00:00   RT  4483
    6 2015-03-24 09:00:00   RT 11112
   library(ggplot2)
   library(stringr)
   ggplot(rtt, aes(x = as.factor(Var1), y = Freq, colour = Var2, group = Var2)) + geom_line(size = 1) +
    xlab("R Vs T") + geom_point() +
    scale_x_discrete(labels = function(x) str_wrap(x, width = 2)) +
    ggtitle("Number of T Vs R - through the day") +
    theme(plot.title=element_text(size=rel(1.2), lineheight = 1 ))
我使用以下代码制作了ggplot线图:

                     Var1 Var2  Freq
    1 2015-03-24 00:00:00   RT   612
    2 2015-03-24 01:00:00   RT    65
    3 2015-03-24 06:00:00   RT    58
    4 2015-03-24 07:00:00   RT  5132
    5 2015-03-24 08:00:00   RT  4483
    6 2015-03-24 09:00:00   RT 11112
   library(ggplot2)
   library(stringr)
   ggplot(rtt, aes(x = as.factor(Var1), y = Freq, colour = Var2, group = Var2)) + geom_line(size = 1) +
    xlab("R Vs T") + geom_point() +
    scale_x_discrete(labels = function(x) str_wrap(x, width = 2)) +
    ggtitle("Number of T Vs R - through the day") +
    theme(plot.title=element_text(size=rel(1.2), lineheight = 1 ))

如何从中删除YMD数据,因为我只需要时间,而不需要x轴上的数据,并且图形中的x轴看起来完全混乱

提取“时间”部分有许多选项。以下列出了一些:

 format(as.POSIXct(str1), '%H:%M:%S')
 [1] "20:00:00"

ggplot
代码可以更改为

 library(ggplot2)
 ggplot(rtt, aes(x= factor(strftime(Var1, format='%H:%M:%S')),
     y= Freq, colour=Var2, group=Var2)) +
     xlab("R Vs T") +
     geom_point() + 
     scale_x_discrete(labels = function(x) str_wrap(x, width = 2)) +
     ggtitle("Number of T Vs R - through the day") +
     theme(plot.title=element_text(size=rel(1.2), lineheight = 1 ))
更新 如果只需要提取“小时”部分

 library(lubridate)
 hour(ymd_hms(str1))
 #[1] 20
数据
str1由于时间仅由小时组成:

library(ggplot2)
rtt$hour <- as.POSIXlt(rtt$Var1)$hour
ggplot(rtt, aes(hour, Freq, col = Var2)) + geom_line()

非常感谢…akrun…您在ggplot上的解决方案奏效了!。。。。当我在数据框中使用它时,您的其他解决方案在创建虚拟变量时对该变量起作用…但由于某种原因,日期仍然保留在表中。但我相信这与我的df有很大关系……再次感谢您……@MageshGovindan感谢您的回复。我不知道你到底是什么意思。也许,您需要将日期指定给转换的日期。比如说<代码>rtt$Var1 oops…仍在了解stackoverflow的欣赏模型的机制…我只是尝试接受这两个答案…再次感谢您的帮助…@MageshGovindan感谢您的回复。是的,一开始可能会让人困惑。
 str1 <- '2015-03-23 20:00:00'

 rtt <- structure(list(Var1 = c("2015-03-24 00:00:00", 
 "2015-03-24 01:00:00", 
 "2015-03-24 06:00:00", "2015-03-24 07:00:00", "2015-03-24 08:00:00", 
 "2015-03-24 09:00:00"), Var2 = c("RT", "RT", "RT", "RT", "RT", 
 "RT"), Freq = c(612L, 65L, 58L, 5132L, 4483L, 11112L)), 
 .Names = c("Var1", "Var2", "Freq"), class = "data.frame",
  row.names = c(NA, -6L))
library(ggplot2)
rtt$hour <- as.POSIXlt(rtt$Var1)$hour
ggplot(rtt, aes(hour, Freq, col = Var2)) + geom_line()
Lines <- "Var1,Var2,Freq
2015-03-24 00:00:00,RT,612
2015-03-24 01:00:00,RT,65
2015-03-24 06:00:00,RT,58
2015-03-24 07:00:00,RT,5132
2015-03-24 08:00:00,RT,4483
2015-03-24 09:00:00,RT,11112"
rtt <- read.csv(text = Lines, as.is = TRUE)