200 likes | 335 Views
R basics workshop. J. Sebasti án Tello Iván Jiménez. Center for Conservation and Sustainable Development Missouri Botanical Garden. 3 fundamental concepts:. Function Argument Object. 2 . Functions and Arguments. Writing in R is like writing in English. Jump three times forward.
E N D
R basics workshop J. Sebastián Tello Iván Jiménez Center forConservation and SustainableDevelopment Missouri Botanical Garden
3 fundamental concepts: • Function • Argument • Object
Writing in R is like writing in English Jump three times forward Action Modifiers
Writing in R is like writing in English Generate a sequence from 5 to 20 with values spaced by 0.5 Action Modifiers
Writing in R is like writing in English Action Modifiers Generate a sequence from 5 to 20 with values spaced by 0.5 seq(from=5, to=20, by=0.5) Function Arguments
Basic anatomy of an R command Open parenthesis Close parenthesis Equal sign Comma seq(from = 5, to = 20, by = 0.5) Other arguments Argument value Argument name Function
Basic anatomy of an R command seq(from=5, to=20, by=0.5) Function Arguments • A function in R defines an action to take, and is similar to a verb in English • Functions apply to arguments, which define on what and how a function will work • Arguments are usually given within parenthesisafter the function
Basic anatomy of an R command • Arguments almost always have names (e.g., "from ", "to", etc.) seq(from=5, to=20, by=0.5) • Names can be eliminated if arguments are given in predetermined order seq(5, 20, 0.5) seq(0.5, 5, 20) • Arguments can be reordered if you use names seq(by=0.5, to=20, from=5)
Basic anatomy of an R command • Writing an R command is like writing a command in English Sum 19, 4, 2, 6 and 2 sum(19, 4, 2, 6, 2) Paste the words “R”, “Basics” and “Worshop” paste("R", "Basics", "Workshop") Repeat “R” 10 times rep(x="R", times=10)
Basic anatomy of an R command • Somefunctions can be used in otherways. • E.g. theoperator+must be usedbetweenvalues Sum 19 and 4 Sum 19 and 4 sum(19, 4) 19 + 4
Arguments with predetermined values seq(from=5, to=20, by=0.5) seq(to=10) • Frequently, functions have arguments with predetermined values • Predetermined arguments do not need to be specified • You can find predetermined values in the help page ?seq
Getting Help • 1. Access help file for the function - RTFM • 2. Make a search in www.rseek.org or Google • 3. Ask a friend • 4. Ask a question in an on-line discussion forum - http://www.r-project.org/mail.html • 5. Have a look at the internal code of the function
Getting Help • 1. Access help file for the function by using ? or help() ?lm help(lm) • Critical components of the help pages: • Usage– How to use the function • Arguments– Description of arguments • Details– Details on how the function works • Value– Description of the output • See Also – Other related functions • Examples – Examples on how to use the function
Getting Help • 2. Make a search in www.rseek.org
Getting Help • 3. Ask a friend (that knows more than you do) Iván Jiménez, Ph.D. Associate Scientist Center for Conservation and Sustainable Development office phone: + 1 (314) 577-6566 fax: + 1 (314) 577-9596 email: ivan.jimenez@mobot.org
Getting Help • 4. Ask a question in an on-line r-project.org/mail.html
Getting Help • 5. Have a look at the internal code of the function • The name of the function without parenthesis produces the R code behind the function; e.g.: lm • Some functions are not written in R and cannot be accessed this way; e.g.: seq
Summary: functions and arguments seq(to=20,by=0.5) Function Arguments Output Predetermined arguments
Exercise 2 Functions and arguments