【发布时间】:2019-08-18 10:51:51
【问题描述】:
我有一个 data.frame,其中有一列 time 和 05:39:18 23-Oct-2016。
我尝试运行 as.POSIXct 创建另一列,但返回 NA。我做了很多事情,但没有任何改变。
structure
(list(id = c(111868L, 111868L, 111868L, 111868L, 111868L,
111868L),
time = c("05:39:18 23-Oct-2016", "08:08:56 23-Oct-2016", "08:50:41 23-Oct-2016", "09:39:41 23-Oct-2016", "10:30:41 23-Oct-2016", "11:11:11 23-Oct-2016"),
lq = c("3", "B", "A", "2", "B", "B"),
lat = c(-20.3108, -20.3103, -20.3108, -20.3098, -20.3091, -20.3087),
lon = c(-40.2825, -40.2822, -40.2861, -40.2815, -40.2804, -40.2802),
error_semimajor_axis = c(770L, 33105L, 4046L, 651L, 1282L, 10379L),
error_semiminor_axis = c(48L, 42L, 31L, 123L, 1077L, 83L),
error_ellipse_orientation = c(109L, 110L, 77L, 147L, 83L, 94L)),
row.names = c(NA, 6L),
class = "data.frame")
srt(l)
'data.frame': 26462 obs. of 8 variables:
$ id : int 111868 111868 111868 111868 111868 111868 111868 111868 111868 111868 ...
$ time : chr "05:39:18 23-Oct-2016" "08:08:56 23-Oct-2016" "08:50:41 23-Oct-2016" "09:39:41 23-Oct-2016" ...
$ lq : chr "3" "B" "A" "2" ...
$ lat : num -20.3 -20.3 -20.3 -20.3 -20.3 ...
$ lon : num -40.3 -40.3 -40.3 -40.3 -40.3 ...
$ error_semimajor_axis : int 770 33105 4046 651 1282 10379 2281 698 8101 1577 ...
$ error_semiminor_axis : int 48 42 31 123 1077 83 808 124 126 110 ...
$ error_ellipse_orientation: int 109 110 77 147 83 94 93 14 105 165 ...
我的数据是遥测数据,覆盖了南大西洋的一个崎岖区域,我不知道是不是这个问题。我尝试使用Sys.setlocale 设置位置,但没有任何效果。
我需要运行这段代码,第一个有效,但是当我运行其他代码时,列变为 NA。
更改日期格式:添加列日期,其中仅包括日期而不是小时::min::sec
从门户检索的原始文件中的时间最初是 GMT 时间。
l$date <- sapply(strsplit(l$time, split=' ', fixed=TRUE), function(x) (x[2]))
l$date <- as.POSIXct(l$date, format = "%d-%b-%Y",tz = "GMT", usetz = TRUE)
l$time <- as.POSIXct(l$time, format = "%H:%M:%S %d-%b-%Y",tz = "GMT", usetz = TRUE)
谢谢!!
【问题讨论】:
-
请通过
dput提供示例数据,如果我在您的示例字符串上运行您的代码,它可以正常工作。 -
您可以看到一些关于如何包含少量数据的想法in this SO question/answer,了解如何制作一个可重复的出色示例。例如,您可以通过将
dput( head(l) )的输出复制到您的问题中来给我们前六行。