4

I am trying to use coordinates function from sp package to transform my data in spatial data. I made sure I deleted all the NAs values and that all my data is numeric, however, i still get the same error. I have checked in the package information about negative values, but it seems should not be the problem

require(adegenet)
require(pegas)       
require(sp)
require(PopGenReport)
require(dplyr)
require(poppr)

Sites.sp <- Locations
SitesNA.sp<-subset(Sites.sp,!(is.na(Sites.sp["Latitude"]) | 
is.na(Sites.sp["Longitude"])))


coordinates(SitesNA.sp) <- ~Latitude+Longitude
as.tibble(SitesNA.sp)

coordinates(SitesNA.sp) <- ~Latitude+Longitude Error in .local(obj, ...) : cannot derive coordinates from non-numeric matrix

enter image description here

dput

structure(list(A/A= c(NA, NA, 174, 173, 164, 152), YEAR = c("2016", "2016", "2014", "2014", "2013", NA), Collector = c("P. Carazo", "P. Carazo", "AubretTeam", "AubretTeam", "UllerTeam", "AubretTeam" ), Population = c("Istria, Pula", "Cres Island, Cres Township", "Ile d'Yeu Saint-sauveur", "Ile d'Yeu Port Joinville", "Vitre", "Taurignan Vieux"), Country = c("Croatia", "Croatia", "FR", "FR", "FR", "FR"), Abbreviation = c("Pula", "Cres Township", "YSS", "YPJ", "VR", "TV"), Latitude = c("44.849504000000003", "44.959245000000003", "46.706412999999998", "46.727950499999999", "48.124009999999998", "43.0276"), Longitude = c("13.849347", "14.404019999999999", "-2.3309148999999998", "-2.3523725999999998", "-1.2143999999999999", "1.1152"), Altitude = c(NA, NA, "NA", "NA", "91", "NA"), No.Samples = c("3", "3", "34", "No samples sent", "20", "30"), X__1 = c(NA, NA, NA, NA, "Geoff to confirm exact location", NA)), .Names = c("A/A", "YEAR", "Collector", "Population", "Country", "Abbreviation", "Latitude", "Longitude", "Altitude", "No.Samples", "X__1"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

4
  • But SitesNA is not the same than SitesNA.sp. This kind of code isn't object-oriented, so .sp is not an attribute or method of SitesNA
    – aldo_tapia
    Feb 8, 2018 at 12:32
  • ado_tapia sorry it was a typo when writing it here, but that is not the problem :) thanks Feb 8, 2018 at 12:40
  • Can you add dput(head(SitesNA)) to trying to reproduce the problem?
    – aldo_tapia
    Feb 8, 2018 at 12:41
  • The answer is certainly that your lat/long data are character but, please look at your workflow as well. It makes very little sense that you are coercing the data to a SpatialPointsDataFrame and then turning it into a tribble, which is no longer a spatial class object. Although, there may be capacity to handle tribble classes as simple features, I just don't know. Feb 8, 2018 at 16:10

1 Answer 1

3

Look the image uploaded by you, also check the data:

dplyr::glimpse(SitesNA)
## Observations: 6
## Variables: 11
## $ `A/A`        <dbl> NA, NA, 174, 173, 164, 152
## $ YEAR         <chr> "2016", "2016", "2014", "2014", "2013", NA
## $ Collector    <chr> "P. Carazo", "P. Carazo", "AubretTeam", "AubretTeam", "UllerTea...
## $ Population   <chr> "Istria, Pula", "Cres Island, Cres Township", "Ile d'Yeu Saint-...
## $ Country      <chr> "Croatia", "Croatia", "FR", "FR", "FR", "FR"
## $ Abbreviation <chr> "Pula", "Cres Township", "YSS", "YPJ", "VR", "TV"
## $ Latitude     <chr> "44.849504000000003", "44.959245000000003", "46.706412999999998...
## $ Longitude    <chr> "13.849347", "14.404019999999999", "-2.3309148999999998", "-2.3...
## $ Altitude     <chr> NA, NA, "NA", "NA", "91", "NA"
## $ No.Samples   <chr> "3", "3", "34", "No samples sent", "20", "30"
## $ X__1         <chr> NA, NA, NA, NA, "Geoff to confirm exact location", NA

Latitude and Longitude are character, so convert them and coordinates() will work:

SitesNA$Latitude <- as.numeric(SitesNA$Latitude)
SitesNA$Longitude <- as.numeric(SitesNA$Longitude)

coordinates(SitesNA) <- ~Latitude + Longitude
4
  • @MaravillasRuiz you welcome. The manner to say thank you in this site is accepting the answer. Cheers
    – aldo_tapia
    Feb 8, 2018 at 13:30
  • 1
    In this case 'as.numeric()' will work however, the R default is characters as factors, which would make 'as.numeric()' not work because it would return the factor level and not the vector value. With coercing a factor to numeric you would need to to do 'as.numeric(as.character(x))' Feb 8, 2018 at 16:06
  • @JeffreyEvans exactly. In that case as.numeric() could be a headache for new r-users
    – aldo_tapia
    Feb 8, 2018 at 16:10
  • I have always found this behavior bizarre. It seems like the default coercion should be returning the values of the vector and there should be a different function for returning the levels of the factor for the entire vector. Historically, this has been argued numerous times but the R core team is insistent that this is the correct way to handle factors. Feb 8, 2018 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.