【问题标题】:How to extract day, month, year, hour and minutes from a large integer如何从大整数中提取日、月、年、小时和分钟
【发布时间】:2020-10-27 18:09:51
【问题描述】:

在我的原始数据文件(4600条记录)中,日期、年、小时和分钟被合并成一个大整数,例如:

1205981254(1998 年 5 月 12 日,12:54)

问题是每个月第 10 天到第 31 天之间的日期记录有 10 位,而第 1 天到第 9 天之间的日期只有 9 位:

905981254(1998 年 5 月 9 日,12:54)

多年前我还是一名学生时创建了这个原始数据文件,并没有遵循特定的格式。如何从这些整数中提取日、月、年和一天中的时间?我已经阅读了所有以前的 Qs 和 As,但没有找到解决我特定问题的方法。

【问题讨论】:

  • 在您的编码方案中,日期为 2001 年 1 月 1 日 1h07 的值是多少?
  • 你所有的日期都是 19xx 吗?还是你跨入 20xx?
  • 所有日期都从 19xx 开始,1999 年 1 月 1 日 1h07 将是 101990107

标签: r datetime


【解决方案1】:

您可以将数据转换回POSIXct/POSIXlt格式:

x <- c(1205981254, 905981254)
x1 <- as.POSIXct(sprintf("%010d", x), format = "%d%m%y%H%M", tz = 'UTC')
x1
#[1] "1998-05-12 12:54:00 UTC" "1998-05-09 12:54:00 UTC"

然后您可以从中提取您想要的任何信息。

#Date      
as.integer(format(x1, "%d"))
#[1] 12  9
#Hour
as.integer(format(x1, "%H"))
#[1] 12 12
#Minute
as.integer(format(x1, "%m"))
#[1] 5 5

【讨论】:

  • 试试这个给定905981254的另一个例子。
  • 错误信息:> test.MR$x1
  • @Dag 在我的回答中似乎与x 一起使用。试试str_padx1 &lt;- as.POSIXct(stringr::str_pad(x, 10, pad = 0), format = "%d%m%y%H%M", tz = 'UTC')
  • 它有效。时区 UTC+2 的合成器如何? tz = 'UTC+2' 不起作用。
  • 你可以使用Etc/GMT-2。在控制台中运行 OlsonNames() 以获取可以使用的有效时区列表。
【解决方案2】:
dates <- c( 1205981254, 905981254 )
#convert to character
dates <- as.character( dates )
#convert to posix, based on length.. add a 0 as prefix in case of 9 character-length
dplyr::if_else( nchar(dates) == 10, 
                as.POSIXct( dates, format = "%d%m%y%H%M"),
                as.POSIXct( paste0(0,dates), format = "%d%m%y%H%M") )

[1] "1998-05-12 12:54:00 CEST" "1998-05-09 12:54:00 CEST"

【讨论】:

    【解决方案3】:

    你可以试试这个:

    v1 <- '1205981254'
    v2 <- '905981254'
    #Extract dates first
    nv1 <- as.Date(v1,'%d%m%y%H%M')
    nv2 <- as.Date(paste0(0,v2),'%d%m%y%H%M')
    #Extract hours
    nh1 <- paste0(substr(v1,nchar(v1)-3,nchar(v1)-2),':',substr(v1,nchar(v1)-1,nchar(v1)),':00')
    nh2 <- paste0(substr(v2,nchar(v2)-3,nchar(v2)-2),':',substr(v2,nchar(v2)-1,nchar(v2)),':00')
    #Concatenate
    ndate1 <- paste0(nv1,' ',nh1)
    ndate2 <- paste0(nv2,' ',nh2)
    #Define as dates
    as.POSIXlt(ndate1,tz = 'GMT')
    as.POSIXlt(ndate2,tz = 'GMT')
    
    [1] "1998-05-12 12:54:00 GMT"
    [1] "1998-05-09 12:54:00 GMT"
    

    【讨论】:

      【解决方案4】:

      如果一年中的世纪保持不变,也许这对你有用。

      library(dplyr)
      #> 
      #> Attache Paket: 'dplyr'
      #> The following objects are masked from 'package:stats':
      #> 
      #>     filter, lag
      #> The following objects are masked from 'package:base':
      #> 
      #>     intersect, setdiff, setequal, union
      
      x <- 905981254
      y <- 1205981254
      
      
      df <- data.frame(records = as.character(rep(c(x, y), 100))) %>% 
        mutate(records = ifelse(nchar(records) == 9, paste("0", records, sep = ""), records)) %>% 
        mutate(records = as.POSIXct(records, format = "%d%m%y%H%M"))
      head(df)
      #>               records
      #> 1 1998-05-09 12:54:00
      #> 2 1998-05-12 12:54:00
      #> 3 1998-05-09 12:54:00
      #> 4 1998-05-12 12:54:00
      #> 5 1998-05-09 12:54:00
      #> 6 1998-05-12 12:54:00
      

      reprex package (v0.3.0) 于 2020 年 7 月 7 日创建

      【讨论】:

        【解决方案5】:

        如果你所有的年份都是19XX 而不是20XX,你可以使用

        dates <- c(1205981254,905981254)
        as.POSIXct(sub("(..)(..)(..)(..)$","-\\1-19\\2 \\3:\\4", dates),format="%d-%m-%Y %H:%M")
        
         "1998-05-12 12:54:00 AST" "1998-05-09 12:54:00 AST"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-26
          • 2015-01-18
          • 1970-01-01
          • 2012-05-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多