R basics workshopJ. Sebastián TelloIván JiménezCenter for Conservation and Sustainable DevelopmentMissouri Botanical Garden
3 fundamental concepts:
• Function
• Argument
• Object
2. Functions and Arguments
Writing in R is like writing in English
Jump three times forward
Action Modifiers
Generate a sequence from 5 to 20 with values spaced by 0.5
Action Modifiers
Writing in R is like writing in English
seq(from=5, to=20, by=0.5)
Action Modifiers
Function Arguments
Generate a sequence from 5 to 20 with values spaced by 0.5
Writing in R is like writing in English
seq(from = 5, to = 20, by = 0.5)
Basic anatomy of an R command
Function
Open parenthesis
Argumentname
Equal sign
Other arguments
CommaClose
parenthesis
Argumentvalue
• 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 parenthesis after the function
seq(from=5, to=20, by=0.5)Function Arguments
Basic anatomy of an R command
• Names can be eliminated if arguments are given in predetermined orderseq(5, 20, 0.5)
seq(from=5, to=20, by=0.5)
• Arguments almost always have names (e.g., "from ", "to", etc.)
seq(by=0.5, to=20, from=5)• Arguments can be reordered if you use names
seq(0.5, 5, 20)
Basic anatomy of an R command
• Writing an R command is like writing a command in English
paste("R", "Basics", "Workshop")
rep(x="R", times=10)
Paste the words “R”, “Basics” and “Worshop”
Repeat “R” 10 times
sum(19, 4, 2, 6, 2)
Sum 19, 4, 2, 6 and 2
Basic anatomy of an R command
• Some functions can be used in other ways.
• E.g. the operator + must be used between values
sum(19, 4)
Sum 19 and 4
19 + 4
Sum 19 and 4
Basic anatomy of an R command
seq(from=5, to=20, by=0.5)
seq(to=10)
?seq
• Frequently, functions have arguments with predetermined values
• Predetermined arguments do not need to be specified
• You can find predetermined values in the help page
Arguments with predetermined values
• 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
?lm• 1. Access help file for the function by using ? or help()
• 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
help(lm)
Getting Help
• 3. Ask a friend (that knows more than you do)
Getting Help
Iván Jiménez, Ph.D.Associate ScientistCenter for Conservation and Sustainable Development
office phone: + 1 (314) 577- 6566fax: + 1 (314) 577-9596email: [email protected]
• 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
seq
• Some functions are not written in R and cannot be accessed this way; e.g.:
Getting Help
ArgumentsFunction
Predetermined argumentsOutput
seq(to=20, by=0.5)
Summary: functions and arguments
Exercise 2Functions and arguments