微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FUNX [[i]],...中的rror:使用st_polygon时is.numericx不为TRUE

如何解决FUNX [[i]],...中的rror:使用st_polygon时is.numericx不为TRUE

这是基于此线程polygons from coordinates

我有一个数据框canopy_coords

> dput(canopy_coords)
structure(list(Transect_ID = c("A","B","C","D","E","F","G","H","I","J","K"),percentage = c(0.141663114074618,0.679361716844141,0.356075131567195,0.0369279244914651,0.545912720728666,0.256667358567938,0.887708339374512,0.355368616292253,0.0197559630032629,0.0667797913774848,0.513462982373312),pt1_rbx = c(864943.815781996,864953.870790847,864962.2814157,864970.487301377,864980.271109795,864991.027427403,865000.351992635,865008.532059795,865017.232489794,865026.778973778,865031.926178827),pt1_rby = c(4173793.09292455,4173792.95070282,4173793.68917041,4173794.85743131,4173794.84731682,4173794.94912696,4173796.92544687,4173800.28061334,4173803.77056191,4173807.13103388,4173809.1564907),pt2_rbx = c(864938.438652263,864943.815781996,865026.778973778),pt2_rby = c(4173793.46951521,4173793.09292455,4173807.13103388),pt3_lbx = c(864938.754229696,864944.008063806,864954.103848622,864962.954663805,864971.586264863,864980.897890762,864991.945752488,865002.172447518,865010.506388562,865018.820648425,865028.048200056),pt3_lby = c(4173789.72813409,4173789.63976124,4173788.97486369,4173789.15990521,4173790.60336356,4173791.370514,4173791.58086304,4173793.66042772,4173797.6375251,4173801.15880759,4173804.0296625),pt4_lbx = c(864944.008063806,865028.048200056,865033.427710242),pt4_lby = c(4173789.63976124,4173804.0296625,4173805.40185838)),class = c("tbl_df","tbl","data.frame"),row.names = c(NA,-11L))

我尝试使用上述线程将其转换为多边形:

polys <- lapply(1:11,function(x){
      # build a matrix of coordinates that enclose the polygon
      pmat <- matrix(c(canopy_coords[x,'pt1_rby'],canopy_coords[x,'pt1_rbx'],'pt2_rby'],'pt2_rbx'],'pt3_lby'],'pt3_lbx'],'pt4_lby'],'pt4_lbx'],'pt1_rbx']),ncol = 2,byrow = TRUE)
      
      ## create polygon objects
      st_polygon(list(pmat),dim = "XY")
    })

不幸的是,这会产生以下错误Error in FUN(X[[i]],...) : is.numeric(x) is not TRUE

当我检查pmat时,结构看起来正确:

> pmat
     [,1]    [,2]    
[1,] 4173793 864943.8
[2,] 4173793 864938.4
[3,] 4173790 864938.8
[4,] 4173790 864944  
[5,] 4173793 864943.8

如果我对st_polygon()函数进行“注释”,则polys对象看起来正确:

> head(polys,5)
[[1]]
     [,] 4173793 864943.8

[[2]]
     [,] 4173793 864953.9
[2,] 4173793 864943.8
[3,] 4173790 864944  
[4,] 4173789 864954.1
[5,] 4173793 864953.9

[[3]]
     [,] 4173794 864962.3
[2,] 4173793 864953.9
[3,] 4173789 864954.1
[4,] 4173789 864963  
[5,] 4173794 864962.3

[[4]]
     [,] 4173795 864970.5
[2,] 4173794 864962.3
[3,] 4173789 864963  
[4,] 4173791 864971.6
[5,] 4173795 864970.5

[[5]]
     [,] 4173795 864980.3
[2,] 4173795 864970.5
[3,] 4173791 864971.6
[4,] 4173791 864980.9
[5,] 4173795 864980.3

寻找任何可能解释发生情况的帮助。

解决方法

这是因为您的对象canopy_coords不仅是data.frame,而且还是tbl

您可以做两件事:

pmat <- matrix(unlist(c(canopy_coords[x,'pt1_rby'],canopy_coords[x,'pt1_rbx'],'pt2_rby'],'pt2_rbx'],'pt3_lby'],'pt3_lbx'],'pt4_lby'],'pt4_lbx'],'pt1_rbx'])),ncol = 2,byrow = TRUE)

class(canopy_coords) <- 'data.frame'

其余部分保持不变。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。