如何在列表中保存100个SpatialLines对象? [英] How to save 100 SpatialLines objects in the list?

查看:58
本文介绍了如何在列表中保存100个SpatialLines对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在列表xySpatialLines中保存100个SpatialLines对象.下面给出的代码提供了一个错误:

I need to save 100 SpatialLines objects in the list xySpatialLines. The below given code provides an error:

xySpatialLines [i] = spl中的错误:无效的类型/长度(S4/0)在 向量分配

Error in xySpatialLines[i] = spl : invalid type/length (S4/0) in vector allocation

library(sp)
xySpatialLines <- NULL

for(i in 1:100)
{
 x <- c(i,5,4,8)
 y <- c(1,3,4,i)
 xy <- cbind(x,y)
 xy.sp = sp::SpatialPoints(xy)
 spl <- SpatialLines(list(Lines(Line(xy.sp), ID=i)))
 xySpatialLines[i] = spl
}

推荐答案

初始化xySpatialLines不是作为NULL而是作为列表,例如由

Initialize xySpatialLines not as NULL but as a list, e.g. by

xySpatialLines <- list()

或者更好的是,预先分配避免增长所需的空间:

or better, pre-allocate the space you need to avoid incremental growth:

xySpatialLines <- vector(mode = "list", length = 100)

,然后执行其余脚本.

这篇关于如何在列表中保存100个SpatialLines对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆