R Course 1st Lecture.

Slides:



Advertisements
Similar presentations
Introduction to R Brody Sandel. Topics Approaching your analysis Basic structure of R Basic programming Plotting Spatial data.
Advertisements

R for Macroecology Aarhus University, Spring 2011.
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
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.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
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.
STAT 534: Statistical Computing Hari Narayanan
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chris Knight Beginners’ workshop.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Lecture 11 Introduction to R and Accessing USGS Data from Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2013 This work was funded by National.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Introduction to R and Data Science Tools in the Microsoft Stack Jamey Johnston.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Introduction to R Chris Free. Introduction to R Free! Superior (if not comparable) to commercial alternatives Available on all platforms Not just for.
16BIT IITR Data Collection Module If you have not already done so, download and install R from download.
Introduction to R and Data Science Tools in the Microsoft Stack Jamey Johnston.
7 - Programming 7J, K, L, M, N, O – Handling Data.
ECE 1304 Introduction to Electrical and Computer Engineering
Topics Designing a Program Input, Processing, and Output
Programming in R Intro, data and programming structures
Introduction to MATLAB
Assignment statement:
Naomi Altman Department of Statistics (Based on notes by J. Lee)
Variables and Arithmetic Operators in JavaScript
Primitive Data, Variables, Loops (Maybe)
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
PH2150 Scientific Computing Skills
Matlab review Matlab is a numerical analysis system
Number and String Operations
Use of Mathematics using Technology (Maltlab)
Lecture 2 Python Programming & Data Types
Programming Funamental slides
Introduction to Programming Using Python PART 2
Lecture 2 Python Programming & Data Types
Introduction to Matlab
INTRODUCTION TO MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MIS2502: Data Analytics Introduction to R and RStudio
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
R Course 2nd lecture.
Topics Designing a Program Input, Processing, and Output
Programming For Big Data
Chapter 3: Selection Structures: Making Decisions
Matlab Training Session 2: Matrix Operations and Relational Operators
R Course 3rd lecture.
Data analysis with R and the tidyverse
Matlab Basics.
R course 4th lecture.
Introduction to C Programming
Introduction to Python
Building Java Programs
Presentation transcript:

R Course 1st Lecture

Information about the Course Matthew Exact - 3rd year MORSE M.Exact@warwick.ac.uk Wednesday 1-2pm R0.41 (Except Week 7 which is R0.39) Slides will be emailed out after class

What is R? Open source, free language for statistical computing Widely used in data science and statistics. Used to apply statistical techniques and visualise data Many packages which can be installed to extend the functionality of R You can either use R or RStudio Found at https://www.r-project.org/ or https://www.rstudio.com/products/rstudio/download/

What can R do? According to a 2015 survey of analytical professionals by Rexer Analytics, 76% of respondents report using R. This is up dramatically from just 23% in 2007. More than a third of respondents (36%) identify R as their primary tool.

Topics we will cover Basics Data types Vectors Vector operations Matrices Matrix operations Factors Lists Data frames Graphics Functions Control structures (if, loop etc.) In depth examples and practice using all above skills

The basics Console Variables e.g. x,y etc. Scripts Comments (##) Data types Operators

R code can be entered into the command line directly or saved to a script, which can be run inside a session using the source() function. To go back to code already entered in console, use up arrow You can run a command directly from a script by placing the cursor inside the command or highlighting the commands and hitting Ctrl-Enter. This will advance the cursor to the next command, where you can hit Ctrl-Enter again to run it, advancing the cursor to the next command… Commands are separated either by a ; or by a new line. R is case sensitive. The # character at the beginning of a line signifies a comment, which is not executed. Commands can extend beyond one line of text. Put operators like + at the end of lines for multi-line commands.

R stores both data and output from data analysis (as well as everything else) in objects. Data are assigned to and stored in objects using the <- or = operator. To print the contents of an object, specify the object’s name alone. A list of all objects in the current session can be obtained with ls() Help files for R functions are accessed by preceding the name of the function with ? (e.g. ?seq). In the help file, we will find a list of Arguments to the function, in a specific order. Values for arguments to functions can be specified either by name or position.

Data types Logical Numeric Character Integer Other less used types are: complex, double (higher precision) and raw

Coercion > as.numeric(TRUE) [1] 1 > as.numeric(FALSE) [1] 0 > as.character(4) [1] "4" > as.numeric("4.5") [1] 4.5 > as.integer("4.5") [1] 4 > as.numeric("Hello") [1] NA

Operators Assignment: x <-10, 2->y Arithmetic: x+y,x-y,x*y,x/y,x^y,x%%y Relational: x<y,x>y,x!=y, x==y, etc. Logical: !, &, &&, |, || Precedence and associativity

Assignment Operator Description Associativity <-,<<-,= Leftwards assignment Right to Left ->,->> Rightwards assignment Left to Right Note, = and <- both assign leftwards, but = is only allowed at the top level (i.e. in the command line)

Arithmetic Operator Description Associativity + Addition Left to Right - Subtraction * Multiplication / Division ^ Exponent Right to Left %% Modulus

# Using R as a calculator 2+3 # assign the number 3 to object called abc abc <- 3 # print contents abc or print(abc)

Task Create a ‘height’ variable with value 3 and a ‘width’ variable with value 6 Assign an ‘area’ variable to be height multiplied by width Then print the area

Relational Operator Description Associativity < Less than Left to Right > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not Equal to

Logical Operator Description Associativity ! Logical NOT Left to Right & Element-wise logical AND && Logical AND | Element wise logical OR || Logical OR

x<3 & y>3 TRUE | FALSE !TRUE !(x>5)

Precedence (highest first) :: $ @ ˆ - + (unary) : %xyz% * / + - (binary) > >= < <= == != ! & && | || ~ (unary and binary) -> ->> = (as assignment) <- <<-

Data structures Homogeneous Heterogeneous One-dimensional Atomic vector List 2-dimensional Matrix Dataframe n-dimensional Array

Atomic One dimensional, must be of the same type Created using c() c(“A”,”B”,”C”)

List One dimensional, can be any types x <- list(c(1,2,3), 100, c(TRUE, FALSE, TRUE), list("a", "b", "c"))

Matrix 2-dimensional, 1 type matrix(data=c(1:9),nrow=3,ncol=3) # create a 2x3 matrix, filling down columns matrix(1:6, nrow=2) # now fill across rows matrix(5:14, nrow=2, byrow=TRUE)

Dataframe 2-dimensional, any type df <- data.frame(name = c("Matt", "Joe", "Chris"),age = c(52, 29, 25),relationshipStatus = c("married", "single", "married"))

Array n-dimensional, same type a <- array(data=c(1:6), dim = c(2,2,2)) gives a 2x2x2 array

Using dim() dim() gives the dimensions of the data structure Now try this on all the examples of all the types of data structure

rep() and seq() to generate vectors To create vectors with a predictable sequence of elements, use rep() for repeating elements and seq() for sequential elements. The expression m:n will generate a vector of integers from m to n # second argument is number of repetitions rep(0, times=3) ## [1] 0 0 0 rep("abc", 4) ## [1] "abc" "abc" "abc" "abc" # from, to, by seq(from=1, to=5, by=2) ## [1] 1 3 5 seq(10, 0, -5) ## [1] 10 5 0 # colon operator 3:7 ## [1] 3 4 5 6 7 # you can nest functions rep(seq(1,3,1), times=2) ## [1] 1 2 3 1 2 3 # each vs times rep(seq(1,3,1), each=2) ## [1] 1 1 2 2 3 3

Next class we will do some tasks related to the content covered this class, so you can practice and become more familiar