【问题标题】:How do I extract only the time parameters from Datetime variable in R?如何仅从 R 中的 Datetime 变量中提取时间参数?
【发布时间】:2015-06-02 07:13:11
【问题描述】:

在 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

我用下面的代码制作了一个ggplot折线图:

   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 轴看起来完全乱码。

【问题讨论】:

    标签: r datetime posixct posixlt


    【解决方案1】:

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

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

    或者

     sub('[^ ]+ ', '', str1)
     #[1] "20:00:00"
    

    或者

     strftime(str1, format='%H:%M:%S')
     #[1] "20:00:00"
    

    或者

     library(lubridate)
     format(ymd_hms(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 <- '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))
    

    【讨论】:

    • 非常感谢......akrun......你在ggplot上的解决方案已经奏效了!......当我使用它创建一个虚拟变量时,你的其他解决方案在这个变量上工作......但由于某种原因,日期仍然保留在表格中......当我在数据框上使用它时。但我确信这与我的 df 有很大关系......再次感谢......
    • @MageshGovindan 感谢您的回复。我不确定你的真正意思。也许,您需要将日期分配给转换后的日期。例如。 rtt$Var1 &lt;- strftime(rtt$Var1, format='%H:%M:%S')
    • 哎呀....仍在弄清楚stackoverflow的升值模型的机制.....我只是试图接受这两个答案....再次感谢您的帮助....
    • @MageshGovindan 感谢您的回复。是的,一开始可能会令人困惑。
    【解决方案2】:

    由于时间仅包含小时:

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

    注意:我们将此用于rtt

    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)
    

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 2012-09-15
      • 2021-02-18
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      相关资源
      最近更新 更多