0

搜索了很多,没有找到回复。

我正在为 qdap 库中的“查找”功能而苦苦挣扎。我在数据框 CityCountry 中有一个城市名称列表;这是代码和str:

CityCountry <- data.frame(City = as.character(rownames(spint)))
CityCountry <- as.character(CityCountry)

str(CityCountry)
chr "c(18, 40, 55, 64, 68, 70, 82, 86, 90, 107, 121, 127, 144, 152, 163, 184, 194, 205, 210, 211, 213, 217, 218, 223, 226, 227, 228,"| __truncated__

spint 是一个最短路径数据框,它使用相关城市名称作为行名。我想抓住这些,用它们新建一个数据框,在数据框routes_lookup中查找每个城市对应的国家。这是 str(routes_lookup) 和我的查找函数:

str(routes_lookup)

'data.frame':   2792 obs. of  2 variables:
 $ City_Dest   : chr  "Buenos Aires" "Buenos Aires" "Mar Del Plata" "Mar Del Plata" ...
 $ Country_Dest: Factor w/ 240 levels "Afghanistan",..: 9 9 9 9 9 9 9 152 152 170 ...

CityCountry$Country <- lookup(CityCountry, routes_lookup)

这是我不断收到的错误。我已经尝试过很多次,但上面的函数调用似乎最接近正确(尽管当然不完全在那里)。

Error in exists(x, envir = envr) : 
variable names are limited to 10000 bytes

我当然认为上面显示的 str(CityCountry) 说明了这个问题。但是数据框包含 chr 类型的列,routes_lookup 中的 City_Dest 列也是如此。如何使这两列具有相同的数据类型?

4

1 回答 1

1

代表 OP 回答了他们自己的问题,如下所示:

注:由原发帖者发帖,复制到这里

弄清楚了。原始数据帧(CountryCity)和查找数据帧(routes_lookup)中的查找值必须是同一类型(并且仅仅因为两者都是字符,前者不能将字符全部粉碎到一个条目中)。另外,“terms”参数的colname必须在函数调用中指定。

spint <- as.data.frame(shortest.paths(g_all))
cities <- as.data.frame(matrix(rownames(spint)), byrow=TRUE)
CityCountry <- data.frame(City = as.character(cities$V1))
routes_lookup <- subset(routes_sa, select=c("City_Source", "Country_Source"))

CityCountry$Country <- lookup(CityCountry$City, routes_lookup)
于 2014-06-09T19:17:43.277 回答