[R] Help me get this function to work...
David Winsemius
dwinsemius at comcast.net
Mon Jul 13 21:08:21 CEST 2009
In R a function only returns the last evaluation, so you need to wrap
up all of the local results into a list at the end of the function.
On Jul 13, 2009, at 1:23 PM, Chip Maney wrote:
>
> 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
> }
> }
# Perhaps something like:
list(ShrubCover.df, TreeCover.df, TotalCover.df)
>
> }
>
>
> Cover.fn("VFFF1-7")
>
> _________________________________________________________________
>
> right from Hotmail. Check it out.
>
> M_WL_QA_HM_celebrity_photos2_072009&cat=celebrity
> ______________________________________________
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list