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 ActionModifiers 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 Argument name Equal sign Other arguments Comma Close parenthesis Argument value
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
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
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
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)))
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
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 Sum 19 and 4 Basic anatomy of an R command
1. Access help file for the function - RTFM 2. Make a search in or Googlewww.rseek.org 3. Ask a friend 4. Ask a question in an on-line discussion forum 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
2. Make a search in Getting Help
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
4. Ask a question in an on-line r-project.org/mail.htmlr-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
Arguments Function Predetermined arguments Output seq(to=20, by=0.5) Summary: functions and arguments
Exercise 2 Functions and arguments
4. Opening/Saving Files
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
To save data: read.table read.csv load To read data: write.table write.csv save Save and Open Data
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
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
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
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
Exercise Opening/saving files