Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers."— Presentation transcript:

1 3. Functions and Arguments

2 Writing in R is like writing in English Jump three times forward Action Modifiers

3 Generate a sequence from 5 to 20 with values spaced by 0.5 ActionModifiers Writing in R is like writing in English

4 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

5 seq(from = 5, to = 20, by = 0.5) Basic anatomy of an R command Function Open parenthesis Argument name Equal sign Other arguments Comma Close parenthesis Argument value

6 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

7 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

8 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

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

10 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

11 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

12 1. Access help file for the function - RTFM 2. Make a search in www.rseek.org or Googlewww.rseek.org 3. Ask a friend 4. Ask a question in an on-line discussion forum - http://www.r-project.org/mail.html http://www.r-project.org/mail.html 5. Have a look at the internal code of the function Getting Help

13 ?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

14 2. Make a search in www.rseek.orgwww.rseek.org Getting Help

15 3. Ask a friend (that knows more than you do) Getting Help Sohee Kang, Ph.D. Math and Stats Learning Centre Center for Teaching and Learning email: soheekang@utsc.uoronto.ca

16 4. Ask a question in an on-line r-project.org/mail.htmlr-project.org/mail.html Getting Help

17 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

18 Arguments Function Predetermined arguments Output seq(to=20, by=0.5) Summary: functions and arguments

19 Exercise 2 Functions and arguments

20 4. Opening/Saving Files

21 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

22 To save data: read.table read.csv load To read data: write.table write.csv save Save and Open Data

23 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

24 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

25 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

26 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

27 Exercise Opening/saving files


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

Similar presentations


Ads by Google