Re: [R] Learning, "if" and "else"

From: Don MacQueen <macq_at_llnl.gov>
Date: Tue 14 Jun 2005 - 09:54:09 EST

There are lots of ways to do this, and lots of sources of information. Go to the R website and download the "Introduction to R" document from the "Manuals" link.

Here is one simple way. Not the most succinct in terms of coding style, perhaps, but relatively easy to understand, and relatively good for beginning to understand the language, in my opinion.

Starting with your lifeform object as shown, with the two variables, dbh and form.

lifeform$biomass <- rep(NA,nrow(lifeform)) is.tree <- lifeform$form=='tree'
lifeform$biomass[is.tree] <-
exp(-4.898+4.512*log(lifeform$dbh)-0.319*(log(lifeform$dbh))^2)[is.tree] is.liana <- lifeform$form=='liana'
lifeform$biomass[is.liana] <-
exp(-4.898+4.512*log(dbh)-0.319*(log(dbh))^2)[is.liana]

To better understand what is going on, type just this:

    lifeform$form=="tree"

You should see a vector of TRUE and FALSE values, 33 of them. But the rules for the "if()" function say that whatever you supply inside the parentheses should reduce to a *single* true or false value. You gave it 33 of them. See the help page for "if", which you can get to using either

   help('if')
or

   ?'if'

The ifelse() function takes a logical vector for its first argument.

At 3:32 PM -0700 6/13/05, Paulo Brando wrote:
>Dear Rs,
>
>I have tried to use conditional expressions to calculate biomass for
>different life forms (trees, lianas, and palms).
>
>Here is an example:
>
>> lifeform
>
> dbh form
>1 30 tree
>2 29 tree
>3 28 tree
>4 27 tree
>5 26 tree
>6 25 tree
>7 24 tree
>8 23 tree
>9 22 tree
>10 21 tree
>11 20 tree
>12 15 liana
>13 14 liana
>14 13 liana
>15 12 liana
>16 11 liana
>17 10 liana
>18 9 liana
>19 8 liana
>20 7 liana
>21 6 liana
>22 5 liana
>23 30 palm
>24 29 palm
>25 28 palm
>26 27 palm
>27 26 palm
>28 25 palm
>29 24 palm
>30 23 palm
>31 22 palm
>32 21 palm
>33 20 palm
>
>### I want to include biomass
>
>lifeform$biomass <-
>
>{
> if(lifeform$form=="tree")
> exp(-4.898+4.512*log(dbh)-0.319*(log(dbh))^2)
> else{
> if (lifeform$form=="liana")
> 10^(0.07 + 2.17 * log10 (dbh))
> else ("NA")
>}
>Warning message:
>the condition has length > 1 and only the first element will be used in:
>if (lifeform$form == "tree") exp(-4.898 + 4.512 * log(dbh) -
>
>
>### But I always get the message warning message above.
>
>
>
>I looked for similar examples in R mail list archive, but they did not
>help a lot.
>
>I am quite new to 'R'. Any material that covers this theme?
>
>Thank you very much!
>
>Paulo
>
>PS. Sorry about the last e-mail. I did not change the message title.
>________________________________________
>Paulo M. Brando
>Instituto de Pesquisa Ambiental da Amazonia (IPAM)
>Santarem, PA, Brasil.
>Av. Rui Barbosa, 136.
>Fone: + 55 93 522 55 38
>www.ipam.org.br
>E-mail: pmbrando@ipam.org.br
>
>______________________________________________
>R-help@stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Jun 14 09:58:19 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:33 EST