简体   繁体   中英

How to fix error in non-numeric matrix in R

I have this data frame:

structure(list(Nazwa = c("a", "b"), Miejscowosc = c("aaa", "bbb"
), KodPocztowy = c("09-520", "44-207"), Zainstalowano = c("2020-03-20 00:00:00.000", 
"2019-02-27 00:00:00.000"), Szczytowa = c(9.14, 4.5), Latitude = c("52.550000", 
"50.101860"), Longitude = c("19.700000", "18.546640")), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

I read this data from the aaa.xlsx file.

points <- read_xlsx(paste0("From/",qwerty,"/aaa.xlsx"))

Then I try to use the code:

points_sp <- SpatialPoints(points[,c(6,7)], CRS("+init=EPSG:4326"))

Unfortunately I am getting an error: cannot derive coordinates from non-numeric matrix How to solve it?

You just need to convert the character strings to numeric and the tbl_df to a matrix or plain data frame:

points$Latitude <- as.numeric(points$Latitude)
points$Longitude <- as.numeric(points$Longitude)
points_sp <- SpatialPoints(as.data.frame(points[,c(6,7)]), CRS("+init=EPSG:4326"))
points_sp
SpatialPoints:
#      Latitude Longitude
# [1,] 52.55000  19.70000
# [2,] 50.10186  18.54664
# Coordinate Reference System (CRS) arguments: +proj=longlat +datum=WGS84 +no_defs 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM