[R] function output with for loop and if statement

aaron wells awells10 at hotmail.com
Thu Apr 23 21:18:47 CEST 2009


Gavin, thank you for the suggestions.  Unfortunately the function is still not working correctly.  Below are the dummy datasets that you requested. In the function dummy.vegdata = vegetation; and dummy.spplist = specieslist.  A little clarification on why the if statement is in the function.  I am using the apply function to sum columns of data that correspond with different lifeforms in order to derive a total cover value for each  lifeform in each plot (plt).  When only one species occurs in a lifeform the apply function doesn't work since there is only one column of data. So, the if statement is an attempt to include the column of data from the dummy.vegdata into the output when there is only one species in a given lifeform.  Examples of this condition in the dummy.vegdata include water (Bare_Ground) and popbal (Deciduous_Tree).
 
 
         Aaron
 
 
> dummy.vegdata

plt water salarb salpul popbal leddec picgla picmar arcuva zygele epiang calpur poaarc pelaph flacuc tomnit hylspl carvag caraqu calcan carsax
T1      0      0   10.0    0.0     20    0.0     35      0    0.0    0.0      0    0.0    5.0    0.0     20     55    0.0      1    5.0      0
T2      0      0    0.0    0.0      3    0.0     62      0    0.0    0.0      0    0.0    8.0    2.0      5     65    0.0      0    3.0      0
T3      0      0    0.0    0.0      0    0.0      1     80    2.0    0.0      4    0.0    0.0    0.0      0      0    0.0      0    0.0      0
T4      0      1    0.0   30.0      0    0.1      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.0      0    0.0      0
T5      0      0    0.0    0.0      0    0.0      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.0      0    0.0      0
T6      0      0    0.0    0.0      0   20.0      0      0    0.0    1.0      0    0.0    2.0    0.1      0     20    0.0      0    0.0      0
T7      0      0    0.0    0.0      5    0.0     40      0    0.0    0.0      0    0.0    5.0    0.0      0     85    0.0      0    0.0      0
T8      0      0    3.0    0.0     40    0.0      5      0    0.0    0.0      0    0.0    0.0    0.0      1      0    0.0      2    0.0      0
T9      0      0    1.0    0.0     20    0.0     10      0    0.0    0.0      0    0.0    0.1    0.0      0      3    0.0      1    0.0      0
T10     0      0    0.0   65.0      0    2.0      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.0      0    5.0      0
T11     0      1    1.0    0.0     30    0.0     45      0    0.0    0.0      0    0.1    1.0    0.0      0     10    1.0      0    0.0      0
T12     0      5    7.0    0.1      0    3.0      5      0    0.0    0.0      0    0.0    0.0    0.0      0      5    1.0      0    2.0      5
T13     0      0   20.0    0.0      5    2.0      5      0    0.0    0.0      0    0.0    0.0    0.0     25      0    2.0      2    3.0     15
T14     0      0   35.0    0.0      0    0.0      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.1      0    3.0      2
T15     0      0    0.0    1.0      0   20.0      0      5    0.1    0.0      0    0.0    0.0    0.0      0     10    0.0      0    0.0      0
T16     0      0    0.0    0.0      1   80.0      0      0    0.0    0.1      0    0.0    1.0    0.0      0     50    0.0      0    0.1      0
T17   100      0    0.0    0.0      0    0.0      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.0      0    0.0      0
T18     0      0    0.0    0.0      0   15.0      0      5    0.0    2.0      0    0.0    1.0    0.0      0     20    0.0      0    1.0      0
T19     0      0    0.0   10.0      0   50.0      0      0    0.0    0.0      0    0.0    0.1    0.0      0     55    0.0      0   20.0      0
T20     0      0    0.0    0.0     45    0.0     15      0    0.0    0.0      0    0.0    1.0    0.0      0     50    0.0      0    0.0      0
T21     0      0    0.0    0.0      0    0.0      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.0      0   35.0      0
T22   100      0    0.0    0.0      0    0.0      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.0      0    0.0      0
T23     0      0    0.0    0.0      0   15.0      0      1    0.0    0.0      0    0.0    0.0    0.0      0      2    0.0      0    0.0      0
T24     0      0    0.1    0.0      0    0.0      0      0    0.0    0.0      0    0.0    0.0    0.0      0      0    0.0      0   25.0      0
T25     0      0    0.0    0.0      0   25.0      0      0    0.0    1.0      0    0.0    0.0    0.0      0     15    0.0      0    2.0      0
 
> dummy.spplist

code                      SciName         LifeForm        Class
water                       Water      Bare_Ground        Other
salarb        Salix_arbusculoides Deciduous_Shrubs     Vascular
salpul   Salix_planifolia_pulchra Deciduous_Shrubs     Vascular
popbal        Populus_balsamifera   Deciduous_Tree     Vascular
leddec            Ledum_decumbens Evergreen_Shrubs     Vascular
picgla               Picea_glauca   Evergreen_Tree     Vascular
picmar              Picea_mariana   Evergreen_Tree     Vascular
arcuva    Arctostaphylos_uva-ursi Evergreen_Shrubs     Vascular
zygele          Zygadenus_elegans            Forbs     Vascular
epiang    Epilobium_angustifolium            Forbs     Vascular
calpur Calamagrostis_purpurascens          Grasses     Vascular
poaarc                Poa_arctica          Grasses     Vascular
pelaph         Peltigera_aphthosa          Lichens Non-Vascular
flacuc    Flavocetraria_cucullata          Lichens Non-Vascular
tomnit         Tomentypnum_nitens           Mosses Non-Vascular
hylspl       Hylocomium_splendens           Mosses Non-Vascular
carvag             Carex_vaginata           Sedges     Vascular
caraqu  Carex_aquatilis_aquatilis           Sedges     Vascular
calcan   Calamagrostis_canadensis          Grasses     Vascular
carsax            Carex_saxatilis           Sedges     Vascular

 
 
> Subject: Re: [R] function output with for loop and if statement
> From: gavin.simpson at ucl.ac.uk
> To: awells10 at hotmail.com
> CC: r-help at r-project.org
> Date: Thu, 23 Apr 2009 09:18:17 +0100
> 
> On Wed, 2009-04-22 at 15:51 -0400, aaron wells wrote:
>> Hello all, turns out i'm having a bad R week. I am at my wits end
>> with a function that I am trying to write. When I run the lines of
>> code outside of a function, I get the desired output. When I wrap the
>> lines of code into a function it doesn't work as expected. Not sure
>> what is going on here. I suspected that the syntax of the if
>> statement with the for loop was the culprit, but when I only ran the
>> part of the code with the for loop with no if statement I still had
>> the above problem (works outside function, fails when wrapped into a
>> function). Below is the code and example output. Please help! 
> 
> It would help a lot if you spaced your code out a bit around key
> operators and structural elements.
> 
> Anyway, you didn't enclose the if/else blocks in {} in such cases, only
> the line following the if(...) is run if the clause is TRUE - the lines
> after that, but the code you provided doesn't even load into R - the
> lone else is a syntax error. So it is a bit difficult to see what is
> going on and you don;t provide an example any of us can reproduce as we
> don't have your data objects.
> 
> I edited your functions below to do what I think you intended and to
> clean it up a bit. Perhaps we can use this as starting point if the
> function here:
> 
> `concov.test` <- function(vegetation, specieslist)
> {
> test.veg <- vegetation
> names(test.veg) <- specieslist$LifeForm
> nams <- unique(names(test.veg))
> tmp <- matrix(nrow = nrow(test.veg), ncol = length(nams))
> for (i in nams) {
> test.out <- apply(test.veg[, names(test.veg)==i], 1, sum)
> tmp.match <- nams[nams==i]
> tmp.col <- match(tmp.match, nams)
> tmp[1:nrow(test.veg), tmp.col] <- test.out
> tmp.out <- data.frame(row.names(test.veg), tmp, row.names = 1)
> names(tmp.out) <- nams
> ## do you need this or is this for debugging?
> print(tmp.out)
> tmp.out.sort <- tmp.out[, order(names(tmp.out))]
> }
> if(table(names(tmp.out))[i] == 1) {
> nams.srt <- names(tmp.out.sort)
> tmp.match2 <- nams.srt[nams.srt == i]
> tmp.col2 <- match(tmp.match2, names.srt)
> tmp.out.sort[1:nrow(test.veg), tmp.col2] <-
> test.veg[, names(test.veg)==i]
> return(tmp.out.sort)
> } else {
> return(tmp.out.sort)
> }
> }
> 
> doesn't do what you want. Please provide a small, reproducible example
> (that means with data, dummy or otherwise) so we can run the code and
> test changes against your data.
> 
> HTH
> 
> G
> 
>> 
>> 
>> Thanks, 
>> 
>> Aaron
>> 
>> 
>> 
>> concov.test<-function(vegetation,specieslist)
>> {
>> test.veg<-vegetation
>> names(test.veg)<-specieslist$LifeForm
>> tmp<-matrix(nrow=nrow(test.veg),ncol=length(unique(names(test.veg))))
>> for (i in unique(names(test.veg))) {test.out<-apply(test.veg[,names(test.veg)==i],1,sum)
>> tmp.match<-unique(names(test.veg))[unique(names(test.veg))==i]
>> tmp.col<-match(tmp.match,unique(names(test.veg)))
>> tmp[1:nrow(test.veg),tmp.col]<-test.out
>> tmp.out<-data.frame(row.names(test.veg),tmp,row.names=1);names(tmp.out)<-unique(names(test.veg))
>> tmp.out
>> tmp.out.sort<-tmp.out[,order(names(tmp.out))]
>> }
>> if(table(names(tmp.out))[i]==1)
>> tmp.match2<-names(tmp.out.sort)[names(tmp.out.sort)==i]
>> tmp.col2<-match(tmp.match2,names(tmp.out.sort))
>> tmp.out.sort[1:nrow(test.veg),tmp.col2]<-test.veg[,names(test.veg)==i]
>> return(tmp.out.sort)
>> else return(tmp.out.sort)
>> }
>> 
>> 
>> 
>> ----Incorrect output when run as function-----
>> 
>> 
>> 
>>> test<-concov.test(ansveg_all,spplist.class)
>>> test
>> Bare_Ground Deciduous_Shrubs Deciduous_Tree Evergreen_Shrubs Evergreen_Tree Forbs Grasses Lichens Mosses Sedges
>> ANSG_T01_01_2008 NA NA NA NA NA NA NA NA 95.0 NA
>> ANSG_T01_02_2008 NA NA NA NA NA NA NA NA 16.0 NA
>> ANSG_T01_03_2008 NA NA NA NA NA NA NA NA 71.0 NA
>> ANSG_T01_04_2008 NA NA NA NA NA NA NA NA 10.0 NA
>> ANSG_T02_01_2008 NA NA NA NA NA NA NA NA 92.2 NA
>> ANSG_T02_02_2008 NA NA NA NA NA NA NA NA 14.0 NA
>> 
>> .
>> 
>> .
>> 
>> .
>> 
>> 
>> 
>> ----Correct output when code is run outside of a function----
>> 
>> 
>> 
>>> test.veg<-ansveg_all
>>> names(test.veg)<-spplist.class$LifeForm
>>> tmp<-matrix(nrow=nrow(test.veg),ncol=length(unique(names(test.veg))))
>>> 
>>> for (i in unique(names(test.veg))) {test.out<-apply(test.veg[,names(test.veg)==i],1,sum)
>> + tmp.match<-unique(names(test.veg))[unique(names(test.veg))==i]
>> + tmp.col<-match(tmp.match,unique(names(test.veg)))
>> + tmp[1:nrow(test.veg),tmp.col]<-test.out
>> + tmp.out<-data.frame(row.names(test.veg),tmp,row.names=1);names(tmp.out)<-unique(names(test.veg))
>> + tmp.out
>> + tmp.out.sort<-tmp.out[,order(names(tmp.out))]
>> + }
>>> if(table(names(tmp.out))[i]==1)
>> + tmp.match2<-names(tmp.out.sort)[names(tmp.out.sort)==i]
>>> tmp.col2<-match(tmp.match2,names(tmp.out.sort))
>>> tmp.out.sort[1:nrow(test.veg),tmp.col2]<-test.veg[,names(test.veg)==i]
>>> return(tmp.out.sort)
>>> else return(tmp.out.sort)
>>> 
>>> 
>>> tmp.out.sort
>> Bare_Ground Deciduous_Shrubs Deciduous_Tree Evergreen_Shrubs Evergreen_Tree Forbs Grasses Lichens Mosses Sedges
>> ANSG_T01_01_2008 0 57.0 1.0 40.0 35.0 22.0 5.0 35.0 95.0 1.1
>> ANSG_T01_02_2008 0 0.0 0.0 0.0 0.0 34.0 0.0 0.0 16.0 24.0
>> ANSG_T01_03_2008 0 31.0 0.0 47.0 1.0 9.1 3.0 3.0 71.0 14.0
>> ANSG_T01_04_2008 0 0.0 0.0 12.0 0.0 13.2 0.0 0.0 10.0 16.0
>> ANSG_T02_01_2008 0 15.0 1.0 22.0 36.0 9.2 2.0 38.0 92.2 0.1
>> ANSG_T02_02_2008 0 33.0 66.0 23.0 2.0 5.0 0.0 3.0 14.0 0.0
>> .
>> 
>> .
>> 
>> .
>> 
>> 
>> _________________________________________________________________
>> Rediscover Hotmail: Get quick friend updates right in your inbox. 
>> 
>> Updates2_042009
>> [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> 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.
> -- 
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> Dr. Gavin Simpson [t] +44 (0)20 7679 0522
> ECRC, UCL Geography, [f] +44 (0)20 7679 0565
> Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
> Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
> UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> 
_________________________________________________________________
Rediscover Hotmail®: Get quick friend updates right in your inbox. 

Updates2_042009



More information about the R-help mailing list