我想要一种简单的方法来计算SpatialLinesDataFrame对象中每一行的坐标的最小值和最大值
代码:
coordinates(contour)
提取SpatialLinesDataFrame对象:
[[9]]
[[9]][[1]]
[,1] [,2]
[1,] -4.44583300 45.87010
[2,] -4.24583300 45.87874
[3,] -4.04583300 45.90037
[4,] -4.02830912 45.90306
[20,] -1.6458330 42.98340
[21,] -1.8458330 43.07336
[[12]]
[[12]][[1]]
[,1] [,2]
[1,] -1.845833 43.48721
[2,] -1.849027 43.50306
[3,] -1.845833 43.50926
[4,] -1.710073 43.70306
[5,] -1.645833 43.74554
[6,] -1.445833 43.73724
[7,] -1.373848 43.70306
[8,] -1.261626 43.50306
[9,] -1.308085 43.30306
[10,] -1.445833 43.17663
[11,] -1.645833 43.16952
[12,] -1.808587 43.30306
[13,] -1.845833 43.48721
[[13]]
[[13]][[1]]
[,1] [,2]
[1,] -1.645833 43.34325
[2,] -1.712682 43.50306
[3,] -1.645833 43.58276
[4,] -1.445833 43.58877
[5,] -1.376018 43.50306
[6,] -1.445833 43.33714
[7,] -1.645833 43.34325
有更简单的方法吗?
编辑:
我举了一个@EDi的例子来说明我想要的内容:
[[1]]
[[1]][[1]]
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 2
min(1,2,3)=1 & min(3,2,2)=2
max(1,2,3)=3 & max(3,2,2)=3
[[1]][[2]]
[,1] [,2]
[1,] 1.05 3.05
[2,] 2.05 2.05
[3,] 3.05 2.05
min(1.05,2.05,3.05)= 1.05 & min(3.05,2.05,2.05)= 2.05
max(1.05,2.05,3.05)= 3 .05 & max(3.05,2.05,2.05)= 3.05
[[2]]
[[2]][[1]]
[,1] [,2]
[1,] 1 1.0
[2,] 2 1.5
[3,] 3 1.0
min(1,2,3)= 1& min(1.0,1.5,1.0)= 1.0
max(1,2,3)= 3 & max(1.0,1.5,1.0)= 1.5
答案 0 :(得分:1)
请注意我是否理解正确...这样的事情?
# Some Lines --------------------------------------------------------------
require(sp)
l1 = cbind(c(1,2,3), c(3,2,2))
l1a = cbind(l1[,1]+.05,l1[,2]+.05)
l2 = cbind(c(1,2,3),c(1,1.5,1))
sl1 = Lines(list(Line(l1), Line(l1a)), ID = 'a')
sl2 = Lines(Line(l2), ID = 'b')
sl = SpatialLines(list(sl1, sl2))
plot(sl, col = c("red", "blue"))
abline(v = 1:3, lty = 'dotted')
abline(h = 1:3, lty = 'dotted')
# Extract min / max of coordinates for each line --------------------------
cc <- coordinates(sl)
foo <- function(y) {
# combine coordinates lines with same ID
ccl <- do.call(rbind, y)
# return min / max
return(c(range(ccl[,1]), range(ccl[,2])))
}
out <- t(sapply(cc, foo))
out
# for each line one row
# from left to right (min(x), max(x), min(y), max(y))
<强>更新强>
根据你的编辑(我不清楚你想要每个线段的范围)我建议:
foo <- function(y) {
return(c(range(y[,1]), range(y[,2])))
}
rapply(cc, foo, how = 'unlist')
matrix(rapply(cc, foo, how = 'unlist'), nrow = 4)
rapply()
也将该功能应用于子列表,matrix()
仅用于格式化。