Skip to content
Prev 186352 / 398500 Next

Help get this simple function to work...

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")