gpt4 book ai didi

r - 从经纬度 R 转换为本地时区

转载 作者:行者123 更新时间:2023-12-03 17:12:31 25 4
gpt4 key购买 nike

我有一个包含很多位置的数据框(大约 30.000),我需要将每个位置的时间转换为本地时间。
我尝试了一些想法,例如 this one , 和 this one .但它们对我不起作用。

我有这样的数据:

dt = data.table(date = c("2018-01-16 22:02:37",
"2018-01-16 22:54:00",
"2018-01-16 23:08:38"),
lat = c(-54.5010,
-54.5246,
-54.5285),
long = c(-25.0433,
-25.0929,
-25.0832))

我期望这个输出:
date                      lat           long
2018-01-16 20:02:37 -54.5010 -25.0433
2018-01-16 20:54:00 -54.5246 -25.0929
2018-01-16 21:08:38 -54.5285 -25.0832

一试:
library(sf)
dt = data.table(date = c("2018-01-16 22:02:37",
"2018-01-16 22:54:00",
"2018-01-16 23:08:38"),
lat = c(-54.5010,
-54.5246,
-54.5285),
long = c(-25.0433,
-25.0929,
-25.0832))

sdf = st_as_sf(dt, coords = c("long", "lat"), crs = 4326)

## import timezones (after extraction) and intersect with spatial points
tzs = st_read("timezones.geojson/combined.json", quiet = TRUE) #HERE DONT WORK
sdf = st_join(sdf, tzs)

## convert timestamps to local time
sdf$timeL = as.POSIXlt(sdf$time1, tz = as.character(sdf$tzid))
sdf$timeL
Cannot open data source timezones.geojson/combined.json

Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
Open failed.

然后我尝试:
library(lutz)
library(sf)
library(purrr)
library(dplyr)

download.file("https://github.com/evansiroky/timezone-boundary-builder/releases/download/2019a/timezones-with-oceans.geojson.zip",
destfile = "tz.zip")
unzip("tz.zip", exdir = "data-raw/dist/")
tz_full <- read_sf("data-raw/dist/combined-with-oceans.json")

但这也不起作用。
Cannot open data source ~/Dropbox/Érika Project/mestrado_R/bhv_loc_R/tables/data-raw/dist/combined-with-oceans.json
Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
Open failed.

我是这样理解的:
library(lutz)

v <- tz_lookup_coords(lat = dt$lat, lon = dt$lon, method = "accurate")
v1<-as.data.frame(v)

输出:
[1] "America/Bahia" "Etc/GMT+3"     "Etc/GMT+3"     "Etc/GMT+3"     "Etc/GMT+3"     "Etc/GMT+3"   

但是有了这个输出,我不知道如何转换时区。

我想在做这样的事情:
v1$tzone <- NA
v1[v1$v == "America/Bahia", "tzone"] <- "+3"
v1[v1$v == "America/Sao_Paulo", "tzone"] <- "+3"
v1[v1$v == "Etc/GMT+2", "tzone"] <- "+2"
v1[v1$v == "Etc/GMT+3", "tzone"] <- "+3"

if (v1$tzone == "+3" ) {
v1$timeBR <- NA
v1$timeBR <- strptime(v1$time, format = "%Y-%m-%d %H:%M:%S")
v1$timeBR <- v1$timeBR -3*3600 #creating a column corresponding to local Brazilian time (UTC -3)
v1$hourBR <- as.POSIXlt(v1$timeBR)$hour
v1 <- v1[!is.na(v1$timeBR),]
}

#But the function not works (I dont know do functions), would gonna be better one function with the two condition +3 and +2

编辑

根据建议:
> library(data.table)
data.table 1.12.8 using 2 threads (see ?getDTthreads). Latest news: r-datatable.com
Warning message:
package ‘data.table’ was built under R version 3.5.2
> library(lutz)
Warning message:
package ‘lutz’ was built under R version 3.5.2
> library(purrr)

Attaching package: ‘purrr’

The following object is masked from ‘package:data.table’:

transpose

> library(lubridate)

Attaching package: ‘lubridate’

The following objects are masked from ‘package:data.table’:

hour, isoweek, mday, minute, month, quarter, second, wday, week, yday, year

The following object is masked from ‘package:base’:

date

#t it is the original data
> head(t$time)
[1] 2017-10-16 17:01:00 2017-10-16 18:35:22 2017-10-16 20:38:54 2017-10-16 21:27:27 2017-10-16 21:43:20
[6] 2017-10-16 23:24:46
27092 Levels: 2016-10-24 15:42:00 2016-10-24 21:03:28 2016-10-24 22:04:35 2016-10-24 23:13:40 ... 2020-01-10 11:34:21
> class(t$time)
[1] "factor"
> date2<-t$time
> class(date2)
[1] "factor"
> date2<- as.character(t$time)
> class(date2)
[1] "character"
> head(date2)
[1] "2017-10-16 17:01:00" "2017-10-16 18:35:22" "2017-10-16 20:38:54" "2017-10-16 21:27:27" "2017-10-16 21:43:20"
[6] "2017-10-16 23:24:46"
> t[, date2 := as.POSIXct(date2, format = "%Y-%m-%d %H:%M:%S", tz = "GMT")][,
+ timezone := tz_lookup_coords(lat = lat, lon = long, method = "accurate")][,
+ new_time := map2(.x = date2, .y = timezone,
+ .f = function(x, y) {with_tz(time = x, tzone = y)})][]
Error in `:=`(date2, as.POSIXct(date2, format = "%Y-%m-%d %H:%M:%S", tz = "GMT")) :
Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").

编辑2

我发现出了什么问题,我的原始数据不是 data.table 和 data.frame 格式!
然后,现在我在数据中有这个列表。我试图在一个新列中转换


编辑 3

现在工作了!感谢大家的评论和帮助
t[, date2 := as.POSIXct(date2, format = "%Y-%m-%d %H:%M:%S", tz = "GMT")]
[,timezone := tz_lookup_coords(lat = lat, lon = lon, method = "accurate")]
[,new_time := map2(.x = date2, .y = timezone,
.f = function(x, y) {with_tz(time = x, tzone = y)})][]

newtime<-do.call(rbind, lapply(t$new_time, as.data.frame))
t$newtime<-paste(newtime$`X[[i]]`)

head(t$newtime)
[1] "2016-10-24 12:42:00" "2016-10-24 18:03:28" "2016-10-24 19:04:35" "2016-10-24 20:13:40" "2016-10-24 21:13:00"
[6] "2016-10-25 02:17:05"


有人知道怎么做吗?
谢谢

最佳答案

我想这就是你想要的。我首先创建了一个日期对象。然后,我用 tz_lookup_coords() 搜索时区正如你所尝试的。然后,我用了 with_tz() ,它在不同的时区获取日期时间。请注意 new_time是一个列表,如 str(dt)表示。

library(data.table)
library(lutz)
library(purrr)
library(lubridate)

dt[, date := as.POSIXct(date, format = "%Y-%m-%d %H:%M:%S", tz = "GMT")][,
timezone := tz_lookup_coords(lat = lat, lon = long, method = "accurate")][,
new_time := map2(.x = date, .y = timezone,
.f = function(x, y) {with_tz(time = x, tzone = y)})][]

# date lat long timezone new_time
#1: 2018-01-16 22:02:37 -54.5010 -25.0433 Etc/GMT+2 2018-01-16 20:02:37
#2: 2018-01-16 22:54:00 -54.5246 -25.0929 Etc/GMT+2 2018-01-16 20:54:00
#3: 2018-01-16 23:08:38 -54.5285 -25.0832 Etc/GMT+2 2018-01-16 21:08:38

#str(dt)
#Classes ‘data.table’ and 'data.frame': 3 obs. of 5 variables:
# $ date : POSIXct, format: "2018-01-16 22:02:37" "2018-01-16 22:54:00" "2018-01-16 23:08:38"
# $ lat : num -54.5 -54.5 -54.5
# $ long : num -25 -25.1 -25.1
# $ timezone: chr "Etc/GMT+2" "Etc/GMT+2" "Etc/GMT+2"
# $ new_time:List of 3
# ..$ : POSIXct, format: "2018-01-16 20:02:37"
# ..$ : POSIXct, format: "2018-01-16 20:54:00"
# ..$ : POSIXct, format: "2018-01-16 21:08:38"

更多帮助

如果您有数据框,也可以使用 tidyverse 方法。我用过你的 dt这里。我将其转换为 data.frame 对象。您需要的最后一个瘦身是 unnest() .然后,您将有时间在专栏中。
setDF(dt) %>% 
mutate(date = as.POSIXct(date, format = "%Y-%m-%d %H:%M:%S", tz = "GMT"),
timezone = tz_lookup_coords(lat = lat, lon = long, method = "accurate"),
new_time = map2(.x = date, .y = timezone,
.f = function(x, y) {with_tz(time = x, tzone = y)})) %>%
unnest(new_time)

date lat long timezone new_time
<dttm> <dbl> <dbl> <chr> <dttm>
1 2018-01-16 22:02:37 -54.5 -25.0 Etc/GMT+2 2018-01-16 20:02:37
2 2018-01-16 22:54:00 -54.5 -25.1 Etc/GMT+2 2018-01-16 20:54:00
3 2018-01-16 23:08:38 -54.5 -25.1 Etc/GMT+2 2018-01-16 21:08:38

> str(foo)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 5 variables:
$ date : POSIXct, format: "2018-01-16 22:02:37" "2018-01-16 22:54:00" "2018-01-16 23:08:38"
$ lat : num -54.5 -54.5 -54.5
$ long : num -25 -25.1 -25.1
$ timezone: chr "Etc/GMT+2" "Etc/GMT+2" "Etc/GMT+2"
$ new_time: POSIXct, format: "2018-01-16 20:02:37" "2018-01-16 20:54:00" "2018-01-16 21:08:38"

关于r - 从经纬度 R 转换为本地时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59833660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com