You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用asPOSIXct转换日期时仅单条记录返回NA的技术求助

时间戳转换为POSIXct时出现NA的问题

数据与转换操作

我有一个包含地理定位观测数据及对应时间戳的dataframe,以下是该数据框的子集:

DET <- structure(list(id = c(200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1), 
date = c("2021-03-03 02:00:00", "2021-03-03 14:00:00", "2021-03-04 02:00:00", 
"2021-03-04 14:00:00", "2021-03-05 02:00:00", "2021-03-05 14:00:00", 
"2021-03-06 02:00:00", "2021-03-06 14:00:00", "2021-03-07 02:00:00", 
"2021-03-07 14:00:00", "2021-03-08 02:00:00", "2021-03-08 14:00:00", 
"2021-03-09 02:00:00", "2021-03-09 14:00:00", "2021-03-10 02:00:00", 
"2021-03-10 14:00:00", "2021-03-11 02:00:00", "2021-03-11 14:00:00", 
"2021-03-12 02:00:00", "2021-03-12 14:00:00", "2021-03-13 02:00:00", 
"2021-03-13 14:00:00", "2021-03-14 02:00:00", "2021-03-14 14:00:00", 
"2021-03-15 02:00:00", "2021-03-15 14:00:00", "2021-03-16 02:00:00", 
"2021-03-16 14:00:00", "2021-03-17 02:00:00", "2021-03-17 14:00:00", 
"2021-03-18 02:00:00")), row.names = 1330:1360, class = "data.frame")

随后我执行以下代码将date列从字符型转换为POSIXct格式:

DET$date <- as.POSIXct(DET$date,format="%Y-%m-%d %H:%M:%S",tz="US/Eastern")

异常情况

上述操作中大部分记录转换正常,但第1352行返回了NA,转换后的异常数据如下:

issue <- structure(list(id = c(200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 
200368.1, 200368.1, 200368.1, 200368.1, 200368.1, 200368.1), 
date = structure(c(1614754800, 1614798000, 1614841200, 1614884400, 
1614927600, 1614970800, 1615014000, 1615057200, 1615100400, 
1615143600, 1615186800, 1615230000, 1615273200, 1615316400, 
1615359600, 1615402800, 1615446000, 1615489200, 1615532400, 
1615575600, 1615618800, 1615662000, NA, 1615744800, 1615788000, 
1615831200, 1615874400, 1615917600, 1615960800, 1616004000, 
1616047200), class = c("POSIXct", "POSIXt"), tzone = "US/Eastern")), row.names = 1330:1360, class = "data.frame")

我检查了该记录的格式,看起来和其他记录完全一致:

> check
           id                date
1350 200368.1 2021-03-13 02:00:00
1351 200368.1 2021-03-13 14:00:00
1352 200368.1 2021-03-14 02:00:00
1353 200368.1 2021-03-14 14:00:00
1354 200368.1 2021-03-15 02:00:00
1355 200368.1 2021-03-15 14:00:00

会话信息

以下是我的R会话信息:

> sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base      

other attached packages:
 [1] purrr_0.3.4          data.table_1.14.2    terra_1.6-17        
 [4] raster_3.6-3         beepr_1.3            dBBMMhomeRange_0.0.9
 [7] tidylog_1.0.2        dplyr_1.0.10         sp_1.5-0            
[10] lubridate_1.8.0      magrittr_2.0.3       remotes_2.4.2       

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9          lattice_0.20-45     tidyr_1.2.1        
 [4] prettyunits_1.1.1   png_0.1-7           class_7.3-20       
 [7] ps_1.7.1            clisymbols_1.2.0    assertthat_0.2.1   
[10] rprojroot_2.0.3     utf8_1.2.2          R6_2.5.1           
[13] plyr_1.8.7          e1071_1.7-11        httr_1.4.4         
[16] ggplot2_3.3.6       pillar_1.8.1        RgoogleMaps_1.4.5.3
[19] rlang_1.0.6         curl_4.3.2          rstudioapi_0.14    
[22] geosphere_1.5-14    callr_3.7.2         rgdal_1.5-32       
[25] stringr_1.4.1       munsell_0.5.0       proxy_0.4-27       
[28] compiler_4.2.1      pkgconfig_2.0.3     pkgbuild_1.3.1     
[31] tidyselect_1.1.2    tibble_3.1.8        codetools_0.2-18   
[34] audio_0.1-10        fansi_1.0.3         crayon_1.5.1       
[37] withr_2.5.0         sf_1.0-8            bitops_1.0-7       
[40] grid_4.2.1          lwgeom_0.2-8        gtable_0.3.1       
[43] lifecycle_1.0.2     DBI_1.1.3           units_0.8-0        
[46] scales_1.2.1        KernSmooth_2.23-20  cli_3.4.1          
[49] stringi_1.7.8       cachem_1.0.6        xml2_1.3.3         
[52] ellipsis_0.3.2      generics_0.1.3      vctrs_0.4.1        
[55] move_4.1.8          rjson_0.2.21        tools_4.2.1        
[58] ggmap_3.0.0         glue_1.6.2          jpeg_0.1-9         
[61] abind_1.4-5         fastmap_1.1.0       processx_3.7.0     
[64] parallel_4.2.1      starsExtra_0.2.7    colorspace_2.0-3   
[67] stars_0.5-6         classInt_0.4-7      memoise_2.0.1  

内容的提问来源于stack exchange,提问作者smok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:31:10