
Mastering R Programming Basics: Syntax, Objects, and Grammar Overview
Explore the key concepts of R programming including rules of syntax, object types, basic operators, and grammar. Understand elements like nouns, verbs, operators, and functions in R to enhance your coding skills.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
R Programming I EPID 799C Fall 2017
Overview 5 Rules of R Syntax Basic Object Types Basic Operators Activity: Dataset Tour
3 Elements of R Syntax Objects: nouns 3 pi Mydata somevariable Operators: verbs + - * / & | %in% Functions: more verbs mean() sd() plot() glm()
2 Rules of R Grammar R evaluates expressions. Expressions are objects (nouns) linked using operators and functions (verbs): Operators link objects side-by-side. 1+2 weight/height^2 data$variable Functions link objects in (optionally) named groups. sum(1,2,3,4) rnorm(n=10, mean=0,sd=1)
The End [Everything else is vocabulary!]
Organizing Syntax Elements 1. Objects Types (structures) Modes (flavors) 2. Operators Assignment Infix operators 3. Functions
Two Important Notes 1. It is important to actually type the code we will use today into Rstudio yourself or you may get errors ( unexpected input , etc.). Examples: PowerPoint and Abobe don t use the same as R Windows filepaths incorrectly use \ (the escape character) instead of / (forward slash). 2. PLEASE interrupt me if you have an error or problem someone else probably does too.
Assignment: = or <- To define an object, use <- or = students <- 20 students students+1 people = students - 5 people [no output] 20 21 [no output] 15 An expression without assignment prints the result but does not modify any objects. An expression with assignment defines an object but does not display the result. 8
1. Basic R Objects Types Atom Vector List Matrix Data Frame Modes Logical Numeric Integer Real Complex Character Factor S4 Raw
Atoms: The Basic Building Block One unit of data my.age = 30 my.name = Nat my.age my.name value Nat 30 object symbol my.name my.age
2. The Six Modes (flavors) Numeric Character Logical RAW Integer Real Complex1 13 8.45 1+1i Cat on a Hat TRUE FALSE <bytecode: 0x00000000136f40b8> 1. By default, R will report a special missing value (NaN) in calculations that return imaginary numbers [or other undefined expressions like 0/0].
Arithmetic Operators All of the basic operators (and order of operations) work like you [should] expect with atoms: 1+1 18-19 100/3 (5*12^3)/15 11%%3 11%/%3 #remainder #divisor
R as a Calculator An RCT enrolled 200 asthma patients. Half of the patients received only a standard-of-care rescue inhaler(SoC), while the other half also received immunotherapy. 68 patients in the SoC group had at least one severe asthma attack over the next month, compared to 23 patients in the immunotherapy group. 1. Calculate the risk of attack in both groups (assign to objects). Use these figures to calculate a risk difference and ratio for the effect of immunotherapy.
Logical Operators If you ask R to evaluate an equation, inequality, or Boolean expression of atoms, it will return TRUE or FALSE: 1 == 2+3 3 < 4 12 >= 13-1 TRUE & FALSE TRUE | FALSE (3<4) &!(FALSE) FALSE TRUE TRUE FALSE TRUE TRUE 14
Vectors: Atoms in Sequence Multiple units of data locker.combo = c(12,24,7) foods = c( Pie , Pizza , Tofu ) Pie Pizza Tofu 12 24 7 foods locker.combo
Arithmetic with Vectors Arithmetic operators can be used on vectors with other vectors or atoms: x = 1:5 x x*2 y = c(5,4,3,2,1) # # apply to all [5] 2 4 6 8 10 [5] 1 2 3 4 5 y x+y # [5] 5 4 3 2 1 # element-wise [5] 6 6 6 6 6
Vectorized Arithmetic The heights and weights of five patients in a cohort study at baseline were 64, 72, 70, 67, 73 inches and 80, 85, 79, 72, and 90 kilograms. 1. Create a height vector and a weight vector containing the data. Convert the height vector to centimeters (1 inch = 2.54 cm). 2. Use vector arithmetic to calculate a patient bmi vector (bmi = weight[kg]/height[cm]^2)
Logic with Vectors Logical operators can also be used on vectors with other vectors or atoms: a = 1:5 a a>2 b = c(3,2,1,3,5) # # apply to all [5] F F T T T [5] 1 2 3 4 5 b a==b a>=b # [5] F T T T T # [5] 5 4 3 2 1 # element-wise [5] F T F F T
Slicing Vectors with Atoms Slice a vector using the square brackets: [] locker.combo[1] foods[2] Pizza 12 Index 1 2 3 1 2 3 Pie Pizza Tofu 12 24 7 foods locker.combo
12 24 7 Slicing Vectors with Vectors locker.combo Slice a vector using square brackets: [] locker.combo[c(1,2)] 12 24 foods[c(3,2,1)] 7 24 12 foods[c(2,2,2,2)] 24 24 24 24 foods[c(FALSE,TRUE,TRUE)] 25 7 # Remember, nothing happens to our original # vector unless we are using an assignment!
Querying Vectors Combine a slice with a logical test to query a vector (return all elements that match a condition): x = c(1,1,2,3,5,8) # step-by-step x >= 5 x[c(F,F,F,F,T,T)] [6] F F F F T T [2] 5 8 # in practice x[x>=5] [2] 5 8
Logical Queries The heights and weights of five patients in a cohort study at baseline were 64, 72, 70, 67, 73 inches and 80, 85, 79, 72, and 90 kilograms. Their names are Sam, Andy, Jamie, Billy, and Casey. 1. Create a vector containing the patient names. 2. Write a query to return all height values over 70. 3. Write a query to return the names of patients that are over 70 inches.
Lists: Mixed Vectors A list is a vector that can have multiple modes (flavors). They work like vectors but are referenced using double brackets: [[ ]] list( A , 1, TRUE) list[[1]] A List are a useful object for complex operations.
Matrices: Organized Vectors Vectors can be connected into a matrix: rbind() cbind() a = c(1,2,3) b = c(4,5,6) rbind(a,b) cbind(a,b)
4 5 6 m Slicing Matrices 7 8 9 Like vectors, matrices can be sliced using []. Give slice instructions for both rows and columns (leave one blank to specify all ), separated by a comma: m = rbind( 4:6, 7:9 ) # stack rows m[1, ] # row 1, all columns 4 5 6 m[ ,2] # all rows, column 2 5 8 m[1:2,2:3] # row 1 to 2, col 2 to 3 5 6 # 8 9
4 5 6 m Slicing with Matrices 7 8 9 Matrices can also sliced by a logical matrix (or by extension, a logical test that returns a logical matrix): m = cbind( c(4,7), c(5,8), c(6,9) ) # same m m[rbind( c(T,F,T),c(F,T,F) ) ] # 4 8 6 m[m%%2==0] # even numbers # 4 8 6 # Note that this approach returns a vector!
4 5 6 m Double Slicing 7 8 9 Remember, output can always be input - you can also slice the result of slice as an alternative specification: m = rbind( 4:6, 7:9 ) # better v = m[1, ] v[2] # v is 4 5 6 # 5 # one step m[1, ][2] # 5
Data Frames: Mixing and Naming Data frames allow you to mix-and-match different modes (flavors) of vectors into a matrix you can reference by name. This is a data set. id = c( A , B , C ) bp = c(115, 120, 130) dx = c(0, 0, 1) data.frame(id,bp,dx)
Slicing Data Frames In addition to using the matrix methods, you can also make references by name using the $ operator: dat$bp dat$bp[2] # 2nd bp variable # bp variable 115 120 130 120 dat$bp[dat$bp>=120] dat$id[dat$bp>=120] 120 130 B C
Creating and Slicing a Dataset The heights and weights of five patients in a cohort study at baseline were 64, 72, 70, 67, 73 inches and 80, 85, 79, 72, and 90 kilograms. Their names are Sam, Andy, Jamie, Billy, and Casey. 1. Create a dataset containing name, height, and weight 2. Write an expression to print Andy s information 3. Write two different expressions to print all the height values [Challenge: four]
Functions: Taking Action Functions enable you to perform tasks. A function takes one or more arguments, separated by commas: mean(dat$bp) # one argument table(dat$id, dat$bp) # two arguments rnorm(n=10,mean=1,sd=2) # named arguments
Activity: Tour the Dataset Download and unzip the Births Dataset, then use the RStudio menu to import the small version of the dataset: births2012_small.csv Use these functions to answer the questions below dim() summary() table() hist() plot() 1. Use an expression with assignment to make a working copy of the dataset with a simpler name 2. How many observations, and how many variables are in the (small) births dataset? 3. What is the average maternal age (mage)? How many mothers have the value 99? 4. Make a histogram of gestational age (WKSGEST). What is the minimum and maximum (non-99) gestational age? 5. How many mothers smoked (CIGDUR)? 6. Make a scatterplot of maternal age versus gestational age.