Download presentation
Presentation is loading. Please wait.
Published byΠαρθενιά Διδασκάλου Modified over 5 years ago
1
R basics workshop J. Sebastián Tello Iván Jiménez
Center for Conservation and Sustainable Development Missouri Botanical Garden
2
3 fundamental concepts:
Function Argument Object
3
2. Functions and Arguments
4
Jump three times forward
Writing in R is like writing in English Jump three times forward Modifiers Action
5
Generate a sequence from 5 to 20 with values spaced by 0.5
Writing in R is like writing in English Generate a sequence from 5 to 20 with values spaced by 0.5 Action Modifiers
6
Generate a sequence from 5 to 20 with values spaced by 0.5
Writing in R is like writing in English Action Modifiers Generate a sequence from 5 to 20 with values spaced by 0.5 Function seq(from=5, to=20, by=0.5) Arguments
7
Basic anatomy of an R command
Open parenthesis Close parenthesis Equal sign Comma seq(from = 5, to = 20, by = 0.5) Other arguments Function Argument name Argument value
8
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 parenthesis after the function
9
Basic anatomy of an R command
1. Arguments almost always have names (e.g., "from ", "to", etc.) seq(from=5, to=20, by=0.5) 2. Names can be eliminated if arguments are given in predetermined order seq(5, 20, 0.5) seq(0.5, 5, 20) 3. Arguments can be reordered if you use names seq(by=0.5, to=20, from=5)
10
Basic anatomy of an R command
seq(from=5, to=20, by=0.5) seq(to=10) 4. 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
11
Basic anatomy of an R command
5. You can use functions to give values to an argument (functions within functions) c(19, 4, 2, 6, 2) mean(x=c(19, 4, 2, 6, 2)) mean(x=19, 4, 2, 6, 2) rnorm(n=50, mean=0, sd=1) rnorm(n=50, mean=3, sd=1) boxplot(x=list(rnorm(n=50, mean=0, sd=1), rnorm(n=50, mean=3, sd=1)))
12
Basic anatomy of an R command
Writing an R command is like writing a command in English rep(x="R", times=10) Repeat “R” 10 times sum(19, 4, 2, 6, 2) Sum 19, 4, 2, 6 and 2 paste("R", "Basics", "Workshop") Paste the words “R”, “Basics” and “Worshop”
13
Basic anatomy of an R command
Some functions can be replaced by operators (more on operators later): E.g. the operator + must be used between values sum(19, 4) Sum 19 and 4 19 + 4 Sum 19 and 4
14
Getting Help 1. Access help file for the function - RTFM
2. Make a search in or Google 3. Ask a friend 4. Ask a question in an on-line discussion forum - 5. Have a look at the internal code of the function
15
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
16
Getting Help 2. Make a search in
17
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) fax: + 1 (314)
18
Getting Help 4. Ask a question in an on-line r-project.org/mail.html
19
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
20
Predetermined arguments
Summary: functions and arguments seq(to=20, by=0.5) Function Arguments Output Predetermined arguments
21
Exercise 2 Functions and arguments
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.