This is in continuation for my previous question on keyword extraction from a string in R: Extract a specific key word from a string in R
I have written the following code that returns the keyword as i wish:
loc <- t1$place
loc <- gsub('"', '', loc)
loc <- gsub(',', '', loc)
for(i in 1:nrow(t1))
country <- word(loc[i], 19, sep=fixed(" : "))
country <- gsub(' }', '', country)
The for loop does not seem to work correctly. When I use the same code insde for loop with hardcoded numbers as shown below:
country <- word(loc[2], 19, sep=fixed(" : "))
country <- gsub(' }', '', country)
The code seems to work. But when I put it through a loop, it gives me an error
Error in word[loc, "start"] : subscript out of bounds
Please help me where it is going wrong.
class(country)
says it is a character type. Is the way I coded the for loop wrong??
Other details: t1 is the dataframe of my table. I used Import dataset to load my file week_tweet_filtered.csv and used the command:
t1 <- week_tweet_filtered
to load the same in t1 variable. I access the place column of my table using t1$place. Also, the place column contains fields of the format:
{ "id" : "94965b2c45386f87", "name" : "New York", "boundingBoxCoordinates" : [ [ { "longitude" : -79.76259, "latitude" : 40.477383 }, { "longitude" : -79.76259, "latitude" : 45.015851 }, { "longitude" : -71.777492, "latitude" : 45.015851 }, { "longitude" : -71.777492, "latitude" : 40.477383 } ] ], "countryCode" : "US", "fullName" : "New York, USA", "boundingBoxType" : "Polygon", "URL" : "https://api.twitter.com/1.1/geo/id/94965b2c45386f87.json", "accessLevel" : 0, "placeType" : "admin", "country" : "United States" }