简体   繁体   English

如何修复R中非数字矩阵中的错误

[英]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.我从aaa.xlsx文件中读取了这些数据。

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:您只需要将字符串转换为数字,将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