日间转换

时间:2017-12-22 17:16:34

标签: r

我有一个data.frame A,其中列 filename 你可以找到一种日期结构,格式如下:

  • 20140925_0 - 2014年5月25日00:00的状态
  • 20140925_1 - 2014年5月25日国家01:00
  • 20140925_10 - 2014年5月25日10:00的状态

我的目标是将文件名转换为类似的内容:

  • 20140925_0 = 201409250000
  • 20140925_1 = 201409250100
  • 20140925_10 = 201409251000

有没有人知道如何转换它?请找一个可重复的例子。

A <- structure(list(X = 1:24, ext = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0.1, 0, 0, 0, 0.01, 0, 0.44, 0, 0, 0, 0, 0, 0, 0), filename = structure(1:24, .Label = c("20140925_0.", 
"20140925_1.", "20140925_10", "20140925_11", "20140925_12", "20140925_13", 
"20140925_14", "20140925_15", "20140925_16", "20140925_17", "20140925_18", 
"20140925_19", "20140925_2.", "20140925_20", "20140925_21", "20140925_22", 
"20140925_23", "20140925_3.", "20140925_4.", "20140925_5.", "20140925_6.", 
"20140925_7.", "20140925_8.", "20140925_9.", "20140926_0.", "20140926_1.", 
"20140926_10", "20140926_11", "20140926_12", "20140926_13", "20140926_14", 
"20140926_15", "20140926_16", "20140926_17", "20140926_18", "20140926_19", 
"20140926_2.", "20140926_20", "20140926_21", "20140926_22", "20140926_23", 
"20140926_3.", "20140926_4.", "20140926_5.", "20140926_6.", "20140926_7.", 
"20141007_9."), class = "factor"), Zlewnia = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Bystrzanka_z_Cisnowa", class = "factor")), .Names = c("X", 
"ext", "filename", "Zlewnia"), row.names = c(NA, 24L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

正如尼古拉给你一个解决方案。以下是使用strptime的另一种解决方案: -

A$filename <- strptime(x = as.character(A$filename),
                       format = "%Y%m%d_%H")


A
    X  ext            filename              Zlewnia
1   1 0.00 2014-09-25 00:00:00 Bystrzanka_z_Cisnowa
2   2 0.00 2014-09-25 01:00:00 Bystrzanka_z_Cisnowa
3   3 0.00 2014-09-25 10:00:00 Bystrzanka_z_Cisnowa
4   4 0.00 2014-09-25 11:00:00 Bystrzanka_z_Cisnowa
5   5 0.00 2014-09-25 12:00:00 Bystrzanka_z_Cisnowa
6   6 0.00 2014-09-25 13:00:00 Bystrzanka_z_Cisnowa
7   7 0.00 2014-09-25 14:00:00 Bystrzanka_z_Cisnowa
8   8 0.00 2014-09-25 15:00:00 Bystrzanka_z_Cisnowa
9   9 0.00 2014-09-25 16:00:00 Bystrzanka_z_Cisnowa
10 10 0.00 2014-09-25 17:00:00 Bystrzanka_z_Cisnowa
11 11 0.10 2014-09-25 18:00:00 Bystrzanka_z_Cisnowa
12 12 0.00 2014-09-25 19:00:00 Bystrzanka_z_Cisnowa
13 13 0.00 2014-09-25 02:00:00 Bystrzanka_z_Cisnowa
14 14 0.00 2014-09-25 20:00:00 Bystrzanka_z_Cisnowa
15 15 0.01 2014-09-25 21:00:00 Bystrzanka_z_Cisnowa
16 16 0.00 2014-09-25 22:00:00 Bystrzanka_z_Cisnowa
17 17 0.44 2014-09-25 23:00:00 Bystrzanka_z_Cisnowa
18 18 0.00 2014-09-25 03:00:00 Bystrzanka_z_Cisnowa
19 19 0.00 2014-09-25 04:00:00 Bystrzanka_z_Cisnowa
20 20 0.00 2014-09-25 05:00:00 Bystrzanka_z_Cisnowa
21 21 0.00 2014-09-25 06:00:00 Bystrzanka_z_Cisnowa
22 22 0.00 2014-09-25 07:00:00 Bystrzanka_z_Cisnowa
23 23 0.00 2014-09-25 08:00:00 Bystrzanka_z_Cisnowa
24 24 0.00 2014-09-25 09:00:00 Bystrzanka_z_Cisnowa