문제

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'
도움이 되었습니까?

해결책

Single square brackets are for subsetting lists. Double square brackets are for getting and setting elements of lists.

So use double square brackets. Demo:

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

a[2] is a list with one element:

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

a[[2]] is just that element:

> a[[2]]
[1] 2
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top