[R] if problem in function
Tord Snall
tord.snall at ebc.uu.se
Thu Dec 12 16:13:03 CET 2002
Dear all,
I have written a function for calculating the volume of a tree (=trad) or
snag (=högst).
The included volume regreesion model includes ten parameter values, which
are tree species specific.
björk.formhöjd.pars is an object which includes the parameter values
(parameter set) for birch (=björk).
There is one row per tree in the data object.
> relev.kols[1:5, c("ObjektID", "CPYTradID", "tree.spec", "Diameter")]
ObjektID CPYTradID tree.spec Diameter
1 2101 2101.2.1 spruce 2
2 2101 2101.2.2 spruce 7
3 2101 2101.2.3 pine 29
4 2101 2101.2.4 pine 38
5 2101 2101.2.5 spruce 9
My problem is that the function (below) does not choose new parameter set
for each row. Instead it now chooses (and uses) the spruce parameter set
for the whole data object (because there is spruce on the first row?).
Could someone please give a hint on what's wrong.
Thanks!
Sincerely,
Tord
relev.kols$vol<- ståvolfunk(relev.kols, tradhogst = "trad")
ståvolfunk<- function(datamat, tradhogst=c("trad", "högst"))
{
# first pick out correct parameter sets. They depend on tree species
# if tree speciec is bjork (birch) -> choose
bjork.formhöjd.pars
# and bjork.höjd.pars as pars1 and pars2, respectively
#
if(datamat$tree.spec == "Björk") {
pars1<- björk.formhöjd.pars
pars2<- björk.höjd.pars
}
# if tree species is pine...
if( datamat$tree.spec == "pine" ) {
pars1<- pine.formhöjd.pars
pars2<- pine.höjd.pars
}
# spruce, lärk, en, obestämt barr, obestämt trad
if(datamat$tree.spec == "spruce" | datamat$tree.spec ==
"Lärk" | datamat$tree.spec == "En" |
datamat$tree.spec == "Obestamt.barr" |
datamat$tree.spec == "Obestamt.trad") {
pars1<- spruce.formhöjd.pars
pars2<- spruce.höjd.pars
pars2<- spruce.höjd.pars
}
# övrigt löv
if(datamat$tree.spec != "Björk" & datamat$tree.spec !=
"pine" & datamat$tree.spec != "spruce" &
datamat$tree.spec != "Lärk" & datamat$tree.spec != "En"
& datamat$tree.spec != "Obestamt.barr" & datamat$tree.spec
!= "Obestamt.trad") {
pars1<- övrlöv.formhöjd.pars
pars2<- övrlöv.höjd.pars
}
# then calculate volume for the tree (=trad) or snag (=högst)
if(tradhogst == "trad"){
tradvol<- exp(Söderberg(datamat, pars1)) *
(datamat$Diameter)^2 * pi / 40000
return(tradvol)
}
if(tradhogst== "högst"){
# som cylinder om höjden är <3 m
if(datamat$LangdHojd<30){
högstvol<- ( (datamat$Diameter/2) /100) ^2 * pi
* (datamatris$LangdHojd/10)
}
# och som kapat trad om höjden är högre
else
# först räknas ursprunglig höjd ut
urspr.tradhöjd<- exp(Söderberg(datamat,
pars2))/10 # omräknat till m
#höjdkvot
i<- datamat$LangdHojd/10 /urspr.tradhöjd
# sedan ursprunglig volym
urspr.tradvol<- exp(Söderberg(datamat, pars1)) *
(datamat$Diameter)^2 * pi / 40000
# och så till sist högstubbens volym
högstvol<- urspr.tradvol* (2*i - i^2)
return(högstvol)
}
}
> relev.kols[1:5, c("ObjektID", "CPYTradID", "tree.spec", "Diameter", "vol")]
ObjektID CPYTradID tree.spec Diameter vol
1 2101 2101.2.1 spruce 2 0.0005896749
2 2101 2101.2.2 spruce 7 0.0164895879
3 2101 2101.2.3 pine 29 0.6840856989
4 2101 2101.2.4 pine 38 1.2839852602
5 2101 2101.2.5 spruce 9 0.0329307304
More information about the R-help
mailing list