在我的原始数据文件(4600 条记录)中,日期、年、小时和分钟合并为一个大整数,例如:
1205981254(1998年5月12日12:54)
问题是每个月第 10 天和第 31 天之间的日期记录有 10 位数字,而第 1 天和第 9 天之间的日期只有 9 位数字:
905981254(1998年5月9日12:54)
多年前我还是学生时创建了这个原始数据文件,没有遵循特定的格式。如何从这些整数中提取日、月、年和时间?我已经阅读了所有以前的 Qs 和 As 没有找到我的特定问题的解决方案。
您可以将数据转换回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"
Run Code Online (Sandbox Code Playgroud)
然后,您可以从中提取您想要的任何信息。
#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
Run Code Online (Sandbox Code Playgroud)