[R] Help get this simple function to work...
jim holtman
jholtman at gmail.com
Mon Jul 13 23:57:55 CEST 2009
Your basic problem is that you are trying to change the value of a
global object (TotalCover.df) from within a function. A better
approach is to pass in the object that you are changing and then
return it as the value of the function. You can then assign it back
to the original object if you want. I think something like this
should work, but the code you have is not executable since some
objects are not defined (e.g., data.df):
#Define Variables
Quadrats.df<-unique(data.df$Quadrat)
TotalCover.df<-cbind(0:750/10,0,0,0,0,0,0)
colnames(TotalCover.df)<-
c("Station","Shrub","Tree","Invasive","Herb","Litter","Bare")
Shrub.df<-data.df[data.df$Layer=="Shrub",]
Tree.df<-data.df[data.df$Layer=="Tree",]
Cover.fn<-function(ID, TC.df){
ShrubCover.df<-Shrub.df[Shrub.df$Quadrat==ID,]
for (j in 1:length(ShrubCover.df[,"Quadrat"])){
for (i in 1:751){
if (TC.df[i,"Station"]>=ShrubCover.df[j,"Start"]
&& TC.df[i,"Station"]<= ShrubCover.df[j,"Stop"])
TC.df[i,"Shrub"]<- 1
}
}
TreeCover.df<-Tree.df[Tree.df$Quadrat==ID,]
for (j in 1:length(TreeCover.df[,"Quadrat"])){
for (i in 1:751){
if (TC.df[i,"Station"]>=TreeCover.df[j,"Start"]
&& TC.df[i,"Station"]<= TreeCover.df[j,"Stop"])
TC.df[i,"Tree"]<- 1
}
}
TC.df # return value
}
Cover.fn("VFFF1-7", TotalCover.df)
On Mon, Jul 13, 2009 at 2:06 PM, chipmaney<chipmaney at hotmail.com> wrote:
>
> I have a function (see below). This function has one object, ID. If I run
> the loops by themselves using a character value (ie,"VFFF1-7") instead of
> the function object, then the loops work fine. However, when I try to
> insert the character value via the function call, it doesn't work. I don't
> get an error, but the TotalCover.df dataframe does not update according to
> the loop criteria. Any obvious problems that you can see?
>
> ################Cover Function#########################
>
> #Define Variables
> Quadrats.df<-unique(data.df$Quadrat)
> TotalCover.df<-cbind(0:750/10,0,0,0,0,0,0)
> colnames(TotalCover.df)<-
> c("Station","Shrub","Tree","Invasive","Herb","Litter","Bare")
> Shrub.df<-data.df[data.df$Layer=="Shrub",]
> Tree.df<-data.df[data.df$Layer=="Tree",]
>
> Cover.fn<-function(ID){
>
> ShrubCover.df<-Shrub.df[Shrub.df$Quadrat==ID,]
> for (j in 1:length(ShrubCover.df[,"Quadrat"])){
> for (i in 1:751){
> if (TotalCover.df[i,"Station"]>=ShrubCover.df[j,"Start"]
> && TotalCover.df[i,"Station"]<= ShrubCover.df[j,"Stop"])
> TotalCover.df[i,"Shrub"]<- 1
> }
> }
> TreeCover.df<-Tree.df[Tree.df$Quadrat==ID,]
> for (j in 1:length(TreeCover.df[,"Quadrat"])){
> for (i in 1:751){
> if (TotalCover.df[i,"Station"]>=TreeCover.df[j,"Start"]
> && TotalCover.df[i,"Station"]<= TreeCover.df[j,"Stop"])
> TotalCover.df[i,"Tree"]<- 1
> }
> }
> }
>
>
> Cover.fn("VFFF1-7")
> --
> View this message in context: http://www.nabble.com/Help-get-this-simple-function-to-work...-tp24466533p24466533.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list