[R-sig-Geo] subset of spatial points

Tom Gottfried tom.gottfried at tum.de
Thu Feb 2 17:52:58 CET 2012


Hi Johannes,

> # joining "Coordinates" to "Species"
> df<- merge(Main,Coordinates,by="Station")
>
> #Replacing NA's with 0 for all species
> Sp <- c("A","B","C","D")
> df.sub<-df[,Sp]
> df.sub[is.na(df.sub)]<-0

I suppose you get an error here. The dimension of the index do not fit 
the dimensions of the indexed object (have a look at the manual "An 
Introduction to R" for example. It's on r-project.org). 
`df.sub[is.na(df.sub),]<-0' should work

> df[,Sp]<-df.sub
>
> # Presence points for single species
> dfm <- melt(df, measure.vars=Sp, variable_name = "Species", na.rm = FALSE)
> df_A <- subset(dfm, value > 0 , select = c(Species, X, Y))
>
> # Spatial Information, Coordinates Transform
> coordinates(df_A) <- c("X", "Y")
> proj4string(df_A) <- CRS("+init=epsg:31467")
> df_B <- spTransform(df_A, CRS("+init=epsg:4326"))
>
> # df_B <- as.data.frame(df_B)
>
> Species.names <- names(table(df_B$Species)[table(df_B$Species)>1])
>
> # Subsetting
> for(i in Species.names){
> 	X <- subset(df_B,Species == i) # 1st approach
>
> 	#X <- df_B[Species == i] # 2nd approach

Same issue as above plus what you corrected in the next line. Keep in 
mind that "[" is a function that looks for object in environments as any 
other function does (and there is no object 'Species'). From ?subset: 
"For data frames, the subset argument works on the rows. Note that 
subset will be evaluated in the data frame, so columns can be referred 
to (by name) as variables in the expression (see the examples)." That's 
what's special about subset() and which does not seem to work with the 
method for SpatialPointsDataFrame.

HTH,
Tom

>
> 	#X <- df_B[df_B$Species == i] # 3rd approach
> 	
> 	print(X)
> 	}
>
>

Am 02.02.2012 17:09, schrieb Johannes Radinger:
> Hello,
>
> I tried to follow your and Ken's suggestions but I still fail and
> I can't find the source for the problem.
>
> I attached the script and the data (path to load the data in the script
> must be changed). Maybe there is just a small mistake
> in the script but I tried to find the problem now for
> some hours but can't find  it... Maybe someone of you
> has some hints and you can help me please.
>
> best regards,
> Johannes
>
>
> -------- Original-Nachricht --------
>> Datum: Thu, 02 Feb 2012 15:24:30 +0100
>> Von: Tom Gottfried<tom.gottfried at tum.de>
>> An: r-sig-geo at r-project.org
>> Betreff: Re: [R-sig-Geo] subset of spatial points
>
>> As a workaround you could do
>>
>> subset(df,df$Attr == i)
>>
>> or what Ken suggested.
>>
>> regards,
>> Tom
>>
>> Am 02.02.2012 12:51, schrieb Johannes Radinger:
>>> 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,
>>> /johannes
>>> --
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> --
>> Technische Universität München
>> Department für Pflanzenwissenschaften
>> Lehrstuhl für Grünlandlehre
>> Alte Akademie 12
>> 85350 Freising / Germany
>> Phone: ++49 (0)8161 715324
>> Fax:   ++49 (0)8161 713243
>> email: tom.gottfried at wzw.tum.de
>> http://www.wzw.tum.de/gruenland
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Technische Universität München
Department für Pflanzenwissenschaften
Lehrstuhl für Grünlandlehre
Alte Akademie 12
85350 Freising / Germany
Phone: ++49 (0)8161 715324
Fax:   ++49 (0)8161 713243
email: tom.gottfried at wzw.tum.de
http://www.wzw.tum.de/gruenland



More information about the R-sig-Geo mailing list