如何将城市名称转换为时区? [英] How do I convert city names to time zones?

查看:136
本文介绍了如何将城市名称转换为时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是重复的,但是我到处都是,似乎找不到任何可以解决我在R中的特定问题的东西.我有一列带有城市名称的列:

Sorry if this is repetitive, but I've looked everywhere and can't seem to find anything that addresses my specific problem in R. I have a column with city names:

cities <-data.frame(c("Sydney", "Dusseldorf", "LidCombe", "Portland"))
colnames(cities)[1]<-"CityName"  

理想情况下,我想在每列城市或时区的纬度/经度上附加一列.我尝试在R中使用"ggmap"包,但我的请求超出了他们每天允许的最大请求数.我找到了将经/纬度"转换为时区的"geonames"软件包,因此,如果我获得了该城市的经/纬度,我应该可以从那里获取.

Ideally I'd like to attach a column with either the lat/long for each city or the time zone. I have tried using the "ggmap" package in R, but my request exceeds the maximum number of requests they allow per day. I found the "geonames" package that converts lat/long to timezones, so if I get the lat/long for the city I should be able to take it from there.

编辑以解决潜在的重复问题:我想使用ggmap包而不进行此操作,因为我的行太多,并且每天的请求数最多.

Edit to address potential duplicate question: I would like to do this without using the ggmap package, as I have too many rows and they have a maximum # of requests per day.

推荐答案

您可以从maps包中的world.cities数据中至少获取许多主要城市.

You can get at least many major cities from the world.cities data in the maps package.

## Changing your data to a vector
cities <- c("Sydney", "Dusseldorf", "LidCombe", "Portland")

## Load up data
library(maps)
data(world.cities)

world.cities[match(cities, world.cities$name), ]
            name country.etc     pop    lat   long capital
36817     Sydney   Australia 4444513 -33.87 151.21       0
10026 Dusseldorf     Germany  573521  51.24   6.79       0
NA          <NA>        <NA>      NA     NA     NA      NA
29625   Portland   Australia    8757 -38.34 141.59       0

注意:不包括LidCombe.
警告:对于许多名称来说,世界上有一个以上的城市.例如

Note: LidCombe was not included.
Warning: For many names, there is more than one world city. For example,

world.cities[grep("Portland", world.cities$name), ]
          name country.etc    pop    lat    long capital
29625 Portland   Australia   8757 -38.34  141.59       0
29626 Portland         USA 542751  45.54 -122.66       0
29627 Portland         USA  62882  43.66  -70.28       0

当然,在美国的两个州是缅因州的波特兰市和俄勒冈州的波特兰市. match只是给出列表中的第一个.为了获得良好的结果,您可能需要使用除名称之外的更多信息.

Of course the two in the USA are Portland, Maine and Portland, Oregon. match is just giving the first one on the list. You may need to use more information than just the name to get a good result.

这篇关于如何将城市名称转换为时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆