转换为 POSIXct 时保持结构
Maintaining structure when converting to POSIXct
我正在尝试将两列 unix 时间转换为实际日期时间。请参阅下面的示例:
df = as.matrix(data.frame(col1 = as.numeric(sample(1316006155:1316009155,10)), col2 = as.numeric(sample(1316006155:1316009155,10))))
as.POSIXct(df,origin = "1970-01-01",tz = "GMT")
虽然上面的方法有效,但我松散了数据的结构(即创建了一个大向量)。我如何维护结构(即分别转换为 POSIXct 的两列)。另外,我想如果我在数据帧中有 unix 时间,我可以使用以下应用代码逐行更改(尽管我知道从上面进行矢量化更快),但这不起作用。为什么?
apply(df,2,function(x) as.POSIXct(x,origin = "1970-01-01",tz = "GMT"))
谢谢
您正在使用 matrix
es,但是 "date"
类 因为 "POSIXct"
在 matrix
中是不可能的。特别是 apply
产生矩阵作为结果,从而将 "fresh" 结果再次强制转换为数字。原因是 matrix()
使用 as.vector
,它实际上有一个 mode=
参数,但 "date", "POSIXct", ...
没有。
因此您可以或者强制转换为"character"
格式:
res1 <- apply(df, 2, function(x) as.character(as.POSIXct(x,origin="1970-01-01", tz="GMT")))
或者,更有效:
res1 <- array(as.character(as.POSIXct(df, origin="1970-01-01", tz="GMT")),
dim=dim(df), dimnames=dimnames(df))
# col1 col2
# [1,] "2011-09-14 13:59:23" "2011-09-14 13:21:50"
# [2,] "2011-09-14 13:55:23" "2011-09-14 13:42:59"
# [3,] "2011-09-14 13:35:31" "2011-09-14 13:18:39"
# [4,] "2011-09-14 13:34:12" "2011-09-14 14:00:24"
# [5,] "2011-09-14 13:36:46" "2011-09-14 13:56:52"
# [6,] "2011-09-14 13:26:28" "2011-09-14 13:47:37"
# [7,] "2011-09-14 13:50:51" "2011-09-14 13:30:53"
# [8,] "2011-09-14 13:35:06" "2011-09-14 13:25:55"
# [9,] "2011-09-14 13:38:01" "2011-09-14 13:37:41"
# [10,] "2011-09-14 13:50:26" "2011-09-14 13:31:26"
str(res1)
# chr [1:10, 1:2] "2011-09-14 13:59:23" "2011-09-14 13:55:23" ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:2] "col1" "col2"
或者, 将矩阵强制转换为 "data.frame"
,这样您就可以使用 lapply
。 A data.frame
允许 "POSIXct"
类.
res2 <- as.data.frame(lapply(as.data.frame(df), function(x)
as.POSIXct(x, origin="1970-01-01", tz="GMT")))
# col1 col2
# 1 2011-09-14 13:59:23 2011-09-14 13:21:50
# 2 2011-09-14 13:55:23 2011-09-14 13:42:59
# 3 2011-09-14 13:35:31 2011-09-14 13:18:39
# 4 2011-09-14 13:34:12 2011-09-14 14:00:24
# 5 2011-09-14 13:36:46 2011-09-14 13:56:52
# 6 2011-09-14 13:26:28 2011-09-14 13:47:37
# 7 2011-09-14 13:50:51 2011-09-14 13:30:53
# 8 2011-09-14 13:35:06 2011-09-14 13:25:55
# 9 2011-09-14 13:38:01 2011-09-14 13:37:41
# 10 2011-09-14 13:50:26 2011-09-14 13:31:26
str(res2)
# 'data.frame': 10 obs. of 2 variables:
# $ col1: POSIXct, format: "2011-09-14 13:59:23" "2011-09-14 13:55:23" ...
# $ col2: POSIXct, format: "2011-09-14 13:21:50" "2011-09-14 13:42:59" ...
数据:
df <- structure(c(1316008763, 1316008523, 1316007331, 1316007252, 1316007406,
1316006788, 1316008251, 1316007306, 1316007481, 1316008226, 1316006510,
1316007779, 1316006319, 1316008824, 1316008612, 1316008057, 1316007053,
1316006755, 1316007461, 1316007086), .Dim = c(10L, 2L), .Dimnames = list(
NULL, c("col1", "col2")))
我正在尝试将两列 unix 时间转换为实际日期时间。请参阅下面的示例:
df = as.matrix(data.frame(col1 = as.numeric(sample(1316006155:1316009155,10)), col2 = as.numeric(sample(1316006155:1316009155,10))))
as.POSIXct(df,origin = "1970-01-01",tz = "GMT")
虽然上面的方法有效,但我松散了数据的结构(即创建了一个大向量)。我如何维护结构(即分别转换为 POSIXct 的两列)。另外,我想如果我在数据帧中有 unix 时间,我可以使用以下应用代码逐行更改(尽管我知道从上面进行矢量化更快),但这不起作用。为什么?
apply(df,2,function(x) as.POSIXct(x,origin = "1970-01-01",tz = "GMT"))
谢谢
您正在使用 matrix
es,但是 "date"
类 因为 "POSIXct"
在 matrix
中是不可能的。特别是 apply
产生矩阵作为结果,从而将 "fresh" 结果再次强制转换为数字。原因是 matrix()
使用 as.vector
,它实际上有一个 mode=
参数,但 "date", "POSIXct", ...
没有。
因此您可以或者强制转换为"character"
格式:
res1 <- apply(df, 2, function(x) as.character(as.POSIXct(x,origin="1970-01-01", tz="GMT")))
或者,更有效:
res1 <- array(as.character(as.POSIXct(df, origin="1970-01-01", tz="GMT")),
dim=dim(df), dimnames=dimnames(df))
# col1 col2
# [1,] "2011-09-14 13:59:23" "2011-09-14 13:21:50"
# [2,] "2011-09-14 13:55:23" "2011-09-14 13:42:59"
# [3,] "2011-09-14 13:35:31" "2011-09-14 13:18:39"
# [4,] "2011-09-14 13:34:12" "2011-09-14 14:00:24"
# [5,] "2011-09-14 13:36:46" "2011-09-14 13:56:52"
# [6,] "2011-09-14 13:26:28" "2011-09-14 13:47:37"
# [7,] "2011-09-14 13:50:51" "2011-09-14 13:30:53"
# [8,] "2011-09-14 13:35:06" "2011-09-14 13:25:55"
# [9,] "2011-09-14 13:38:01" "2011-09-14 13:37:41"
# [10,] "2011-09-14 13:50:26" "2011-09-14 13:31:26"
str(res1)
# chr [1:10, 1:2] "2011-09-14 13:59:23" "2011-09-14 13:55:23" ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:2] "col1" "col2"
或者, 将矩阵强制转换为 "data.frame"
,这样您就可以使用 lapply
。 A data.frame
允许 "POSIXct"
类.
res2 <- as.data.frame(lapply(as.data.frame(df), function(x)
as.POSIXct(x, origin="1970-01-01", tz="GMT")))
# col1 col2
# 1 2011-09-14 13:59:23 2011-09-14 13:21:50
# 2 2011-09-14 13:55:23 2011-09-14 13:42:59
# 3 2011-09-14 13:35:31 2011-09-14 13:18:39
# 4 2011-09-14 13:34:12 2011-09-14 14:00:24
# 5 2011-09-14 13:36:46 2011-09-14 13:56:52
# 6 2011-09-14 13:26:28 2011-09-14 13:47:37
# 7 2011-09-14 13:50:51 2011-09-14 13:30:53
# 8 2011-09-14 13:35:06 2011-09-14 13:25:55
# 9 2011-09-14 13:38:01 2011-09-14 13:37:41
# 10 2011-09-14 13:50:26 2011-09-14 13:31:26
str(res2)
# 'data.frame': 10 obs. of 2 variables:
# $ col1: POSIXct, format: "2011-09-14 13:59:23" "2011-09-14 13:55:23" ...
# $ col2: POSIXct, format: "2011-09-14 13:21:50" "2011-09-14 13:42:59" ...
数据:
df <- structure(c(1316008763, 1316008523, 1316007331, 1316007252, 1316007406,
1316006788, 1316008251, 1316007306, 1316007481, 1316008226, 1316006510,
1316007779, 1316006319, 1316008824, 1316008612, 1316008057, 1316007053,
1316006755, 1316007461, 1316007086), .Dim = c(10L, 2L), .Dimnames = list(
NULL, c("col1", "col2")))