0

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?

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
cavaunpeu
  • 614
  • 1
  • 6
  • 16
  • You didn't provide a reproducible example. But my guess is that you don't want CityCountry to be a single character string. That's probably what's causing the problem. – Dason Apr 30 '14 at 16:41
  • to change from a factor to a character try `routes_lookup$Country_Dest <- as.character(routes_lookup$Country_Dest)` – Gary Weissman Apr 30 '14 at 16:42
  • Yes Dason! This would be the problem. Reproducible example is tough without the data I think? Maybe not. Anyways, how do I coerce it into being a 1 column data frame of 293 rows (there's 293 city names) ? – cavaunpeu Apr 30 '14 at 16:45
  • Please make this reproducible `head` and `dput` with `CityCountry` and `routes_lookup` would help a lot. I think Gary is on the righ track as `hr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" ... > str(as.character(rownames(CO2)))` gives me `chr [1:84] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" ...` Also I don't see how row names that are numbers can be looked up in a table that doesn't have numbers. – Tyler Rinker Apr 30 '14 at 17:11
  • Ah OK! I can certainly make reproducible with mtcars if needed. Sry, new around here. I've successfully converted it into a data frame of 1 col, 293 rows with cities <- as.data.frame(matrix(rownames(spint)), byrow=TRUE). This gives me factors. Whether I have this column and the 2 columns in routes_lookup as factors, or this column and other 2 as characters, I still can't get it to work.. – cavaunpeu Apr 30 '14 at 17:16

1 Answers1

1

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)
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519