1

I need to work with several shapefiles in a project in R.

Therefore i would like to iterate the readOGR function to load these files in a list. Later I can use plyr or construct a loop to do the same operation on every shapefile in the list.

here is a minimum reproducible example:

library("rgdal")

setwd("your.path.here")
download.file("http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_0_countries.zip","ne_10m_admin_0_countries.zip")

unzip("ne_10m_admin_0_countries.zip")

# works like this:
my.shapefile<-readOGR("your.path.here","ne_10m_admin_0_countries")
plot(my.shapefile)

# does not work like this
shapefile.list<-list(length=20)
shapefile.list[1]<-readOGR("your.path.here","ne_10m_admin_0_countries")

plot(shapefile.list[1])

Error message is

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y'
4

1 回答 1

1

单方括号用于子集列表。双方括号用于获取和设置列表元素。

所以使用双方括号。演示:

> a=list(1,2,3,4)

a[2]是一个包含一个元素的列表:

> a[2]
[[1]]
[1] 2

a[[2]]就是那个元素:

> a[[2]]
[1] 2
于 2014-03-27T13:12:06.317 回答