[英]Converting non-numeric matrix into numeric
我有一個包含經度和緯度的導入表,並希望從中創建一個空間網格。 坐標采用以下格式:
[1] 6.955 6.937 6.956 6.923
所以看起來對我來說是數字。 但是嘗試創建網格時會發生此錯誤:
.local(obj,...)中的錯誤:
無法從非數值矩陣得出坐標
當我嘗試使用as.numeric更改格式時,得到以下結果:
[1] 10 9 6 8
如何在不更改值的情況下將其轉換為數字格式?
longitude = structure(c(9L, 7L, 10L, 3L, 8L, 4L, 6L, 2L, 11L, 1L, 5L, 9L,
7L, 10L, 3L, 8L, 4L, 6L, 2L, 11L, 1L, 5L), .Label = c("6.920",
"6.922", "6.923", "6.924", "6.926", "6.936", "6.937", "6.939",
"6.955", "6.956", "6.958"), class = "factor")
latitude = structure(c(10L, 9L, 6L, 8L, 1L, 5L, 3L, 2L, 7L, 8L, 4L, 10L,
9L, 6L, 8L, 1L, 5L, 3L, 2L, 7L, 8L, 4L), .Label = c("50.911",
"50.918", "50.920", "50.929", "50.930", "50.931", "50.934", "50.950",
"50.965", "50.969"), class = "factor")
原始表luftdaten.set
包含緯度,經度和顆粒物值。 我試圖創建一個網格:
coordinates(luftdaten.set) <- ~ latitude + longitude
我還嘗試創建僅包含坐標的數據框,並嘗試從中獲取網格:
luftdaten.grid <- data.frame(latitude, longitude)
luftdaten.grid <- data.frame(latitude, longitude)
兩者都拋出所描述的錯誤。
coordinates
位於SpatialTools的“ sp”包中。
這樣工作:
longitude <- as.numeric(as.character(longitude))
latitude <- as.numeric(as.character(latitude))
luftdaten.grid <- data.frame(latitude, longitude)
attach(luftdaten.grid)
coordinates(luftdaten.grid) <- ~ latitude + longitude
假設您要獲取一個矩陣,其中每一行都是經緯度對:
as.matrix(cbind(as.numeric(as.character(latitude)),
as.numeric(as.character(longitude))))
[,1] [,2]
[1,] 50.969 6.955
[2,] 50.965 6.937
[3,] 50.931 6.956
[4,] 50.950 6.923
[5,] 50.911 6.939
[6,] 50.930 6.924
[7,] 50.920 6.936
[8,] 50.918 6.922
[9,] 50.934 6.958
[10,] 50.950 6.920
[11,] 50.929 6.926
[12,] 50.969 6.955
[13,] 50.965 6.937
[14,] 50.931 6.956
[15,] 50.950 6.923
[16,] 50.911 6.939
[17,] 50.930 6.924
[18,] 50.920 6.936
[19,] 50.918 6.922
[20,] 50.934 6.958
[21,] 50.950 6.920
[22,] 50.929 6.926
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.