0

I have the following dataset:

longitude   latitude
NA  NA
NA  NA
NA  NA
NA  NA
-73.77384042    40.64582098
NA  NA
NA  NA
NA  NA
NA  NA
NA  NA

I am using the below code for converting the lat & long to get the location using the below code:

tweets1.df$textAddress <- mapply(FUN = function(lon, lat) revgeocode(c(lon, lat)), tweets1.df$longitude, tweets1.df$latitude)

But I am getting the following error: Error: is.numeric(location) && length(location) == 2 is not TRUE

Please let me know where I am going wrong.

GoRion
  • 23
  • 4
  • Look at `str`; are your variables numeric? Also, you could probably just do `apply(tweets1.df, 1, revgeocode)`, but you need to specify packages so your question is [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If it really is the `NA`s, wrap the vectors you pass (`tweets.df$*`) in `na.omit` or construct an `ifelse`. – alistaire Oct 14 '16 at 05:56
  • e.g. `apply(df, 1, function(x){ifelse(any(is.na(x)), NA, revgeocode(x))})` – alistaire Oct 14 '16 at 06:02
  • @alistaire: The `revgeocode` takes both the long and lat as inputs. Using your example. I am not able to figure out how can I do it. Could you please tell me with `x` & `y` variable – GoRion Oct 14 '16 at 06:28
  • `x` will be a vector of both variables, which is what the function needs. – alistaire Oct 14 '16 at 15:29

1 Answers1

0

This should work:

options(digits=16)
indices <- which(complete.cases(tweets1.df[,1:2]))
tweets1.df$textAddress <- NA
tweets1.df[indices,]$textAddress <- mapply(FUN = function(lon, lat) 
                    revgeocode(c(lon, lat)), 
                    tweets1.df[indices,]$longitude, 
                    tweets1.df[indices,]$latitude)

tweets1.df


longitude    latitude                        textAddress
1            NA          NA                               <NA>
2            NA          NA                               <NA>
3            NA          NA                               <NA>
4            NA          NA                               <NA>
5  -73.77384042 40.64582098 Terminal 5, Jamaica, NY 11430, USA
6            NA          NA                               <NA>
7            NA          NA                               <NA>
8            NA          NA                               <NA>
9            NA          NA                               <NA>
10           NA          NA                               <NA>

you can try this too, a little modification of what suggested in the comments:

tweets1.df$textAddress <- apply(tweets1.df, 1, function(x){print(x);ifelse(any(is.na(x)), NA, revgeocode(x))})
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63