簡體   English   中英

如何修復R中非數字矩陣中的錯誤

[英]How to fix error in non-numeric matrix in R

我有這個數據框:

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"))

我從aaa.xlsx文件中讀取了這些數據。

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

然后我嘗試使用代碼:

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

不幸的是,我收到一個錯誤:無法從非數字矩陣導出坐標 如何解決?

您只需要將字符串轉換為數字,將tbl_df轉換為矩陣或普通數據框:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM