错误:在R模块中的ggmap下执行mapdist时is.character(from)

时间:2016-01-20 16:19:59

标签: r

我能够正确执行以下查询 -

> mapdist("newyork", "las vegas, nevada")

      from            to       m       km    miles seconds  minutes    hours
1 newyork las vegas, nevada 4059059 4059.059 2522.299  130967 2182.783 36.37972

但是当我上传名为“Cities.CSV”的下面文件时,我没有得到想要的结果 -

  Org_city          Dest_city
New york        Las vegas, nevada
Washington DC       California
Pittsburg       Kansas City


> city = read.csv("cities.csv")
> dist = mapdist(cities$Org_city, Dest_city)

我得到的错误是 -

Error: is.character(from) is not TRUE

我在R中安装了所有必需的软件包。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

1)您将内容从read.csv保存到变量city,但您在命令中引用变量cities

2)你需要为to城市做同样的事情。

这应该有效,更改sep以匹配您的文件,这是假设标签。

cities <- read.csv("cities.csv",sep="\t",stringsAsFactors=FALSE)
data.frame(t(sapply(1:nrow(cities),
    function(x) mapdist(cities$Org_city[x],cities$Dest_city[x]))))

           from                to       m       km    miles seconds  minutes    hours
1      New york Las vegas, nevada 4059059 4059.059 2522.299  130967 2182.783 36.37972
2 Washington DC        California 4497903 4497.903 2794.997  146008 2433.467 40.55778
3     Pittsburg       Kansas City 1353746 1353.746 841.2178   45116 751.9333 12.53222