[R] Help me get this function to work...

Chip Maney chipmaney at hotmail.com
Mon Jul 13 19:23:57 CEST 2009


I have a function (see below).  This function has one object, ID.  If I run the loops by itself using a character value (ie,"VFFF1-7"), 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")

_________________________________________________________________

 right from Hotmail. Check it out.

M_WL_QA_HM_celebrity_photos2_072009&cat=celebrity



More information about the R-help mailing list