Naomi Altman Department of Statistics (Based on notes by J. Lee)

Slides:



Advertisements
Similar presentations
R for Macroecology Aarhus University, Spring 2011.
Advertisements

MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
PL/SQL.
Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Baburao Kamble (Ph.D) University of Nebraska-Lincoln Data Analysis Using R Week2: Data Structure, Types and Manipulation in R.
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.
Introduction to to R Emily Kalah Gade University of Washington Credit to Kristin Siebel for development of much of this PowerPoint.
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.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
S-PLUS Lecture 2 Jaeyong Lee Department of Statistics Pennsylvania State University.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
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
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
S-PLUS Lecture 4 Jaeyong Lee Pennsylvania State University.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Department of Computer Science Western Michigan University
The R Book Chapter 2: Essentials of the R Language Session 7.
Numbers in ‘C’ Two general categories: Integers Floats
ECE 1304 Introduction to Electrical and Computer Engineering
EEE 161 Applied Electromagnetics
Programming in R Intro, data and programming structures
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
© 2016 Pearson Education, Ltd. All rights reserved.
Documentation Need to have documentation in all programs
Stats Lab #1 TA: Kyle Davis
EGR 115 Introduction to Computing for Engineers
Other Kinds of Arrays Chapter 11
Naomi Altman Department of Statistics (Based on notes by J. Lee)
Intro to PHP & Variables
MATLAB DENC 2533 ECADD LAB 9.
MATLAB: Structures and File I/O
R Programming I EPID 799C Fall 2017.
Arrays, For loop While loop Do while loop
7.3 Matrices.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Python
CHAPTER THREE Sequences.
Use of Mathematics using Technology (Maltlab)
Matlab tutorial course
Lecture 2 Introduction to MATLAB
Vectorized Code, Logical Indexing
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Statistics 540 Computing in Statistics
Data Types and Data Structures
Arrays and Matrices in MATLAB
Introduction to Matlab
Python Primer 1: Types and Operators
INTRODUCTION TO MATLAB
Presented By Farheen Sultana Ist Year I SEM
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.
JavaScript.
Dr Tripty Singh Arrays.
WEEK-2.
Announcements P3 due today
R Course 1st Lecture.
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Matrices Introduction.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES AND OPERATIONS
Matrices and Determinants
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
ECE 120 Midterm 1 HKN Review Session.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Naomi Altman Department of Statistics (Based on notes by J. Lee) R Lecture 2 Naomi Altman Department of Statistics (Based on notes by J. Lee)

Naming Conventions Names in R are made up of upper- and lower-case alphabets. 0-9 and period, ‘.’ can be used except in the first position. R is case sensitive.

Language Layout R commands are either expressions or assignments. 1 – pi +exp(1.7) #prints the output a = 6 #saves "a" = <- are both assignment ‘#’ marks the rest of line as a comment. If a command is not complete, R issues ‘+’ rather than ‘>’.

Vectors There are no scalars; vectors of length 1 are used instead. Vectors are made up of numeric, logical values, or character strings. But you can not mix them. Character strings can be entered with either double or single quotes, but will always be printed with double quotes.

Vector Arithmetic Arithmetic operations on vectors are performed element by element. If two vectors in the same expression have different lengths, the expression will produce a vector with the same length as the longer in the expression. The shorter vectors are recycled until they match the length of the longest.

Logical Vectors Possible elements of a logical vector are T (True) and F (False). Logical operators are >, >=, <, <=, ==, !=. c1&c2 (intersection), c1|c2 (union), !c1 (negation). A logical vector can be used in ordinary arithmetic, in which case F and T become 0 and 1, respectively.

Using Logical and Numerical Vectors x=1:20 x^2 3*x y=seq(0,1,.3) y x+y z=x>15 z 2*z z+y x[z] z[x]

Missing Value Marker, NA When an element of a vector is "not available" or "missing", the special value NA can be used to reserve the place. In general, any operation on an NA becomes an NA. There are also 2 other special elements NaN (not a number) and Inf (infinity)

Session: NA x = 1:5 is.na(x) x[3] = NA x-2 log(x-2)

Session: Character Vector a = c("abc", "def", "ghi") paste("abc", "def) paste("abc", "def", sep="") paste(c("X","Y"),1:10,sep="")

Index Vectors Subsets of elements of a vector can by selected by appending an index vector in square brackets to the name of the vector An index expression can appear on the receiving side of an assignment expression. There are four kinds of index vectors: a logical vector (in this case, the lengths of the vector and index vector must match.) a positive integer vector a negative integer vector a character string vector.

Session: Using Indices x=1:20 x x[c(3,5,6)] x[-c(3,5,6)] ind=x>14 x[ind] x[x>14]

Matrices and Dataframes A matrix is a rectangular array. It can be viewed as a collection of column vectors all of the same length and the same type (i.e. numeric, character or logical). A dataframe is a rectangular array. All of the columns must be the same length, but may be different types. The rows and columns of a vector or dataframe can be given names.

Session: Matrices and Dataframes tab=cbind(x,y,z) tab mode(tab[,1]) tab=data.frame(x,y,z) mode(tab[,3]) rownames(tab) colnames(tab) rownames(tab)= paste("a",1:10) x=1:10 y=rnorm(10,0,1) z=paste("a",1:10) x y z mat=cbind(x,y) mat rownames(mat) colnames(mat) mat[1,] mat[,1] mat[, "x"] mode(mat[,1])

Lists A list is a collection of objects that may be the same or different types. The objects generally have names, and may be indexed either by name or component number. e.g. the output of the t.test function is stored in a list that includes: the t-value (numeric vector of length 1) the null value (a numeric vector of length 1) the alternative hypothesis (a character vector) a confidence interval (a list including the ends of the interval and the confidence level) A dataframe can be treated as a list of column vectors.

Objects A object is a vector, array or list along with a class. The class is a flag that can be used for object oriented programming. It is used to create "methods". e.g. "plot" could do a boxplot on a t-test output object and a residual plot on a regression output object.

Intrinsic Attributes: mode and length Every object in R has two attributes, mode and length. The mode of an object says what type of object it is. The function attributes(x) gives all non-intrinsic attributes of x. There is a special attribute called class which was introduced to enhance object-oriented programming. We will talk about this later.

Session: Intrinsic Attributes x=rnorm(100,3,2) t.out=t.test(x,mu=1) attributes(x) class(x) attributes(t.out) length(t.out) class(t.out) names(t.out) t.out[[1]] t.out$conf t.out #printing is affected by the class unclass(t.out)