[R-sig-Geo] subset of spatial points

Ken katakagi at bu.edu
Thu Feb 2 14:48:25 CET 2012



Johannes Radinger <JRadinger <at> 
gmx.at> writes:

> 
> Hello,
> 
> I tried to subset a spatial points 
dataframe with:
> subset(df,Attr == i)
> but then I get the error:
> "Fehler in 
subset.SpatialPointsDataFrame(df, Attr == 
i) : 
>   Objekt 'Attr' nicht gefunden"
> 
> so it the object 'Attr' couldn't be found. 
There is definetly
> a column called 'Attr' in the 
spatialpointsdataframe. This is also
> proofed when I transform my spatial 
points into a ordinary dataframe before 
everything works (there is the
> column 'Attr'):
> df <- as.data.frame(df)
> 
> It gets strange now:
> when I tried to make a short sample 
script... everything
> works, so I don't know why the 'Attr' 
couldn't be found in 
> my script although it exists...??
> 
> library(rgdal)
> 
> X <- c(9.3182,9.31609,9.29932, 9.14613, 
9.32783)
> Y <- c(54.6461, 54.439, 54.4786, 
54.6561, 54.5668)
> Attr <- factor(c("A","A","B","C","C"))
> 
> df <- data.frame(X,Y,Attr)
> coordinates(df) <- c("X", "Y")
> proj4string(df) <- CRS("+proj=longlat 
+datum=WGS84 +ellps=WGS84 
+towgs84=0,0,0")
> 
> select.Attr<- 
names(table(df$Attr)[table(df$Attr)>1])
> 
> for(i in select.Attr){
> 	X <- subset(df,Attr == i)
> 	print(X)
> 	}
> 
> Any suggestions?
> 
> best regards,
> /johannees

Johannes,
 if spdf is a spatial points data frame, you 
can try:

spdf.subset = spdf[spdf$Attr == i, ]

HTH,
ken



More information about the R-sig-Geo mailing list