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

Slides:



Advertisements
Similar presentations
A gentle introduction to R – how to load in data and produce summary statistics BRC MH Bioinformatics group.
Advertisements

An Introduction to R: Logic & Basics. The R language Command line Can be executed within a terminal Within Emacs using ESS (Emacs Speaks Statistics)
How I work Write code Notepad (or similar), and copy and paste to R Read data always to object dat.
Data in R. General form of data ID numberSexWeightLengthDiseased… 112m … 256f3.61 NA1… 3……………… 4……………… n91m5.1711… NOTE: A DATASET IS NOT A MATRIX!
Basics of Using R Xiao He 1. AGENDA 1.What is R? 2.Basic operations 3.Different types of data objects 4.Importing data 5.Basic data manipulation 2.
An Introduction to R POL 51 October 13, 2008 Malcolm Easton Office hours: Thursday 3:30-5:30 in Rm
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
R Data Import/Export Dr. Jieh-Shan George YEH
LATTICE TECHNOLOGY, INC. For Version 10.0 and later XVL Web Master Advanced Tutorial For Version 10.0 and later.
Quantitative Research in Education Sohee Kang Ph.D., lecturer Math and Statistics Learning Centre.
732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of.
Data, graphics, and programming in R 28.1, 30.1, Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen.
Chapter 6 Generating Form Letters, Mailing Labels, and a Directory
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to to R Emily Kalah Gade University of Washington Credit to Kristin Siebel for development of much of this PowerPoint.
Introduction to R Part 2. Working Directory The working directory is where you are currently saving data in R. What is the current working directory?
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Session 3: More features of R and the Central Limit Theorem Class web site: Statistics for Microarray Data Analysis.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Data Objects in R Vector1 dimensionAll elements have the same data types Data types: numeric, character logic, factor Matrix2 dimensions Array2 or more.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Piotr Wolski Introduction to R. Topics What is R? Sample session How to install R? Minimum you have to know to work in R Data objects in R and how to.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Linux+ Guide to Linux Certification, Third Edition
R Programming Yang, Yufei. Normal distribution.
R packages/libraries Data input/output Rachel Carroll Department of Public Health Sciences, MUSC Computing for Research I, Spring 2014.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Introduction to R Introductions What is R? RStudio Layout Summary Statistics Your First R Graph 17 September 2014 Sherubtse Training.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
R Data Import/Export Dr. Jieh-Shan George YEH
Introduction to R Carol Bult The Jackson Laboratory Functional Genomics (BMB550) Spring 2011.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
STAT 534: Statistical Computing Hari Narayanan
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 Chapter 3: Getting Started with Tasks 3.1 Introduction to Task Dialogs 3.2 Creating a Listing Report 3.3 Creating a Frequency Report 3.4 Creating a Two-Way.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
 A macro represents a commonly used group of statements in the source programming language.  The macro processor replaces each macro instruction with.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Review: A Computational View Programming language common concepts: 1. sequence of instructions -> order of operations important 2. conditional structures.
Introduction to R and Data Science Tools in the Microsoft Stack Jamey Johnston.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Control Structures Hara URL:
Review > x[-c(1,4,6)] > Y[1:3,2:8] > island.data fishData$weight[1] > fishData[fishData$weight < 20 & fishData$condition.
16BIT IITR Data Collection Module If you have not already done so, download and install R from download.
Working with data in R 2 Fish 552: Lecture 3. Recommended Reading An Introduction to R (R Development Core Team) –
Introduction to R and Data Science Tools in the Microsoft Stack Jamey Johnston.
Lecture 2 Functions Trevor A. Branch FISH 553 Advanced R School of Aquatic and Fishery Sciences University of Washington.
R basics workshop Sohee Kang Math and Stats Learning Centre
DATA MANAGEMENT MODULE: Getting Data Into and Out of R
R basics workshop Sohee Kang Math and Stats Learning Centre
DATA MANAGEMENT MODULE: Getting Data Into and Out of R
Python I/O.
Use of Mathematics using Technology (Maltlab)
C Preprocessor(CPP).
A poorly named Curt Hill utility program
funCTIONs and Data Import/Export
Basics of R, Ch Functions Help Managing your Objects
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Islamic University of Gaza
Introduction to javadoc
CSCI N207 Data Analysis Using Spreadsheet
R Course 1st Lecture.
Stat 251 (2009, Summer) Lab 2 TA: Yu, Chi Wai.
R basics workshop J. Sebastián Tello Iván Jiménez
Matlab Basics Tutorial
Presentation transcript:

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