【问题标题】:How to change 3:00 to minutes:seconds date time in R?如何将 3:00 更改为 R 中的分钟:秒日期时间?
【发布时间】:2022-01-28 22:10:20
【问题描述】:

我在 R 中有一个数据集,其中包含以下格式的分钟和秒:

3:00

现在它是一个字符,我怎样才能将它更改为日期时间,以便可以在图表中进行分析?我想分析 Instagram 上的帖子。我有他们的喜欢和视频时间,并想绘制它们。我不能这样做,除非我相信我把它变成了日期时间?

【问题讨论】:

    标签: r date time


    【解决方案1】:

    当我们只有分钟和秒时,不清楚日期时间是什么意思,但这两者分别将 x 变成了时间对象 x1 和 lubridate Period 对象 x2。在内部,时间对象是一天的一小部分,而 as.numeric(x1) 将给出该部分。 as.numeric(x2, unit = "sec") 将给出 Period 对象对应的秒数。

    library(chron)
    x <- "3:00"
    x1 <- times(paste0("0:", x))
    
    library(lubridate)
    x2 <- ms(x)
    

    【讨论】:

      【解决方案2】:

      我想你的数据框看起来像我创建的这个例子。

      df <- data.frame(time= c("3:00", "4:00", "2:00", "1:00", "6:00", "3:30", "1:10"), video= c(1:7),
                       likes= c(10, 17, 6, 25, 14, 5, 19))
      
      library(lubridate)
      library(tidyverse)
      

      我建议使用 lubridate 包转换为秒。

      df2 <- df %>%
        mutate(time_sec= as.period(ms(time), unit = "sec"))
      

      那么您可以考虑将其呈现为带有喜欢与时间的散点图。

      ggplot(df2, aes(x= likes, y= time_sec))+ geom_point(size=2, shape=23)
      

      【讨论】:

        【解决方案3】:

        您可以将时间转换为十进制。

        t2dec <- \(x, type=c('mins', 'secs')) {
          type <- match.arg(type)
          r <- x |> 
            strsplit(':') |>
            do.call(what=rbind) |>
            type.convert(as.is=TRUE)
          if (type == 'mins') {
            r[, 1] + r[, 2]/60
          } else {
            r[, 1]*60 + r[, 2]
          }
        }
        
        t2dec(dat$time)  ## as minutes (default)
        # [1]  1.583333  6.066667  7.366667  8.283333  8.850000 15.616667 23.383333 28.033333 33.350000 36.150000
        # [11] 37.550000 40.250000 41.933333 42.700000 43.550000 47.833333 49.283333 55.066667 56.600000 59.883333
        
        t2dec(dat$time, 's')  ## as seconds (since "s" matches "seconds")
        # [1]   95  364  442  497  531  937 1403 1682 2001 2169 2253 2415 2516 2562 2613 2870 2957 3304 3396 3593
        

        因此:

        op <- par(mfrow=c(1, 2))
        plot(t2dec(dat$time), dat$x, type='b', col=4, xlab='mins', ylab='likes', main='Videos')
        abline(h=0)
        plot(t2dec(dat$time, 's'), dat$x, type='b', col=4, xlab='secs', ylab='likes', main='Videos')
        abline(h=0)
        par(op)
        

        注意:使用 R >= 4.1。


        数据:

        dat <- structure(list(time = c("01:35", "06:04", "07:22", "08:17", "08:51", 
        "15:37", "23:23", "28:02", "33:21", "36:09", "37:33", "40:15", 
        "41:56", "42:42", "43:33", "47:50", "49:17", "55:04", "56:36", 
        "59:53"), x = c(-2L, 2L, -3L, 3L, 7L, 7L, -3L, 0L, 1L, 5L, 3L, 
        0L, 1L, 10L, 1L, 1L, 6L, 3L, -1L, -1L)), class = "data.frame", row.names = c(NA, 
        -20L))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-30
          • 2016-03-18
          • 2020-10-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多