I'm new in R and I'm trying to learn.
I have a giant dataframe from Gbif (900.000 rows) with name of my species and decimalLongitude and decimalLatitude but when I tried to create a SpatialPointDataframe as always with coordinates(data.frame) <- ~ lon lat function (I changed before the name of columns from decimalLongitude in lon ecc... and I deleted the name of species, so I have only a "lon, lat and species=1" as always in my analysis):
Here is the head() function from my dataframe
lon lat species
1 4.841.452 5317168 1
2 10.380.893 48888992 1
3 12.999.839 52388378 1
4 1.002.614 53190685 1
5 8.820.133 50520302 1
and R replied: #Error in .local(obj, ...): cannot derive coordinates from non-numeric matrix
So I see the dataframe and I noticed that my coordinates are a little bit strange.
Do you have some ideas? I need to convert the coordinates?
I looked but I didn't find a solution.
Thank you for spend your time for answer me.
I hope you have a good day and good work!
CodePudding user response:
The problem you have is that the lon column is of class character and not of class numeric. So you need to convert this column. Please find one possible solution (cf. reprex below).
Reprex
- Your data
points <- read.table(text = "lon lat species
1 4.841.452 5317168 1
2 10.380.893 48888992 1
3 12.999.839 52388378 1
4 1.002.614 53190685 1
5 8.820.133 50520302 1", header = TRUE)
- Code
library(raster)
# Convert 'lon' column into numeric
points$lon <- as.numeric(gsub("\\." ,"", points$lon))
# converting to SpatialPointsDataFrame
coordinates(points) <- ~lon lat
- Output
points
#> class : SpatialPointsDataFrame
#> features : 5
#> extent : 1002614, 12999839, 5317168, 53190685 (xmin, xmax, ymin, ymax)
#> crs : NA
#> variables : 1
#> names : species
#> min values : 1
#> max values : 1
Created on 2021-12-10 by the reprex package (v2.0.1)
