Вопрос

Searched heavily and couldn't find a response.

I'm struggling with the "lookup" function in the qdap library. I have a list of city names in data frame CityCountry; here's the code and 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 is a shortest paths data frame, which uses the city names in question as the rownames. I want to grab these, use them to create a new data frame, lookup the country corresponding to each city in data frame routes_lookup. Here's the str(routes_lookup) and my lookup function:

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)

Here's the error I keep getting. I've tried toying with it plenty, but the above function call seems to be closest to correct (although of course not quite there).

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

I would certainly think the problem is illustrated by str(CityCountry) shown above. But the data frame contains a column of type chr, and so does the City_Dest column in routes_lookup. How do I make these two columns of identical data types?

Это было полезно?

Решение

Answered on behalf of the OP who answered their own question as follows:

Note: posted by the original poster and just copied here

Figured it out. Lookup values in original data frame (CountryCity) and lookup data frame (routes_lookup) must be of same type (and just because both are characters, the former can't have the characters all smashed into one entry). In addition, the colname of the "terms" parameter must be specified in the function call.

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top