3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action...

27
3. Functions and Arguments

Transcript of 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action...

Page 1: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

3. Functions and Arguments

Page 2: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

Writing in R is like writing in English

Jump three times forward

Action Modifiers

Page 3: 3. 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

Page 4: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

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

Page 5: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

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

Page 6: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 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

Page 7: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

2. Names can be eliminated if arguments are given in predetermined order

seq(5, 20, 0.5)

seq(from=5, to=20, by=0.5)

1. Arguments almost always have names (e.g., "from ", "to", etc.)

seq(by=0.5, to=20, from=5)3. Arguments can be reordered if you use names

seq(0.5, 5, 20)

Basic anatomy of an R command

Page 8: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

seq(from=5, to=20, by=0.5)

seq(to=10)

?seq

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

Basic anatomy of an R command

Page 9: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

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))

Basic anatomy of an R command

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)))

Page 10: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 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(c(19, 4, 2, 6, 2))

Sum 19, 4, 2, 6 and 2

Basic anatomy of an R command

Page 11: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 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

Basic anatomy of an R command

Page 12: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 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

Page 13: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

?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

Page 14: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 2. Make a search in www.rseek.org

Getting Help

Page 15: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 3. Ask a friend (that knows more than you do)

Getting Help

Sohee Kang, Ph.D.Math and Stats Learning CentreCenter for Teaching and Learning

email: [email protected]

Page 16: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 4. Ask a question in an on-line r-project.org/mail.html

Getting Help

Page 17: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• 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

Page 18: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

ArgumentsFunction

Predetermined argumentsOutput

seq(to=20, by=0.5)

Summary: functions and arguments

Page 19: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

Exercise 2Functions and arguments

Page 20: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

4. Opening/Saving Files

Page 21: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

The Working Directory

• To know what the working directory is:

getwd()

• To modify the working directory:

setwd("C:/MyFiles/Are/InThisFolder")

• Also you can go to File and use the Change dir…option

• The working directory is a folder in your computer where R will search for files to open and where it will save file

Page 22: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• To save data:

read.tableread.csvload

• To read data:

write.tablewrite.csvsave

Save and Open Data

Page 23: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• To save data tables :

?write.table

• Main arguments:

• x: is the R object that you want to save – usually a

vector, matrix or data frame

• file: is the name and location of the file you want to

create

• sep: defines the character that separates columns;

frequently “,” o “\t”

Save Data Tables

Page 24: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

M <- matrix(rnorm(100), ncol=5)

colnames(M) <- 1:ncol(M)

M

save.as <- "matrix_M.txt"

save.as <- "folder_test/matrix_M.txt"

write.table(x=M, file=save.as, sep="\t")

Save Data Tables

Page 25: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

• To read data tables :

read.table

• Main arguments:

• file: where the file is located and what its name is

• header: TRUE or FALSE, whether the first row are the

names of the variables

• sep: defines the character that separates columns;

frequently “,” o “\t”

Open Data Tables

Page 26: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

Data <- read.table(file = file.choose(), header=TRUE, sep="\t")

Data <- read.table(file = "matrix_M.txt", header=TRUE, sep="\t")

class(Data)

names(Data)

Open Data Tables

Page 27: 3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.

ExerciseOpening/saving files