Download presentation
Presentation is loading. Please wait.
1
Stats Lab #1 TA: Kyle Davis
9/4/9
2
Welcome To R First, open R on your computers
Try using R as a basic calculator Examples: Addition: _ + _ Subtraction: _ - _ Multiplication: _ * _ Division: _ / _ Other stuff: _ * _ + _ / _
3
Other things Trying using exponents:3^3, 54^643
General note: R almost never cares about spaces, so use (or omit) spaces in whatever way makes things easiest for you to read.
4
Variables Store a variable
Note: The = sign doesn't mean equal; it means ASSIGN (or, make equal). It assigns whatever's on the right to be the VALUE of the variable on the left.
5
Try making your own variables
Example: x=3 Numberofbeerslastnight=12 Beersinakeg=? Partypeople=? Beersinakeg/partypeople
6
Other rules But variable names must start with a letter, and can't contain special symbols Try these and see if they work 3x = _ = _
7
Store a variable as a result of a computation
X=_*_ Now try: x+5 Name a new variable: y=_/_ X*Y
8
Functions A function computes some function of the values you give to it. The function, and its name, already exist within R. The way you write it is functionName(value1, value2, ...) Try the sum() function sum(3,5,6) sum(x,4,y,x) The prod() function does products prod(_,_,_)
9
You can combine functions with other operations and assignments.
z = sum(2, 3*5, 7) * 2 + prod(3,4)
10
Logarithms Who knows what a logarithm is?
How many people think they will be using them? Well you can if you want in R Log(_) Exp(_)
11
Vectors A special function is the c() function, which means concatenate. c() takes a set of numbers or vectors and concatenates them into one vector. c isn't a function in the sense that it computes something, but it is a function in the R sense, because it takes a set of inputs and turns them into a specific set of outputs. c(2,4,5,6,3) x = c(_,_,_) x
12
Other vector stuff You can also concatenate whole vectors c(x,12,x) In statistics we use vectors to represent sets of measurements (e.g. a score for each person). You can do arithmetic with vectors. Try x+x
13
Scalars Adding or multiplying by a scalar applies that operation to every component of your vector. (Note: a scalar is a single number, i.e. not a vector.) (Note: a component means one entry in a vector or matrix.) You would do this if you had to transform a set of data, e.g. fah = cel * 9/5 + 32; min = sec/60
14
+ and * vectors Adding or multiplying two vectors is done componentwise. You would do this if you needed to combine two variables. exam1 + exam2 daysWorked * hoursPerDay
15
The : operator Often you want a vector of the form c(1,2,3,4,5,...)
The : does this 1:5 x = _ 1:x It can start and end anywhere -7:-2 It can have a fractional part .5:6.5
16
Components of Vectors Sometimes you want to see just one or few components of a vector. To do this, you use square brackets x = c(_,_,_,_,_) x[5] x[2] x[3]*x[4] The entry inside the brackets is called the INDEX.
17
Components of Vectors You can use a vector for the index to get multiple components. y = c(1,2,3) x[y] x[1:3] The output is always in the same order as your index x[c(5,3,1)]
18
Truth Values If you input a statement, R gives you a result of TRUE or FALSE. TRUE and FALSE are not variables because you can't define them; it's best to think of them as special (logical) numbers. 1 < 2 2 > 7 2*6 > 9-4 x = 4 y = 3 x > y
19
Equalities If you want to evaluate an equality (a statement that two things are equal) use == (Remember, single = means assign, not equals.) 1 == 1 2*3 == 6 4 == 2^2 1+1 == 3
20
If one side of your statement is a vector, R evaluates the equation for every component, and returns a vector of TRUEs and FALSEs examScore = c(_,_,_,_,_) cutoff = _ examScore > cutoff
21
If both sides are vectors, then component 1 on the right side is compared to component 1 on the left side, and so on. preTest = c(_,_,_,_,_) postTest = c(_,_,_,_,_) postTest > preTest postTest == preTest
22
A statement of vectors must use vectors of the same length
x = c(_,_,_,_,_) y = c(_,_,_,_,_,_,_) x == y
23
Using True and False as Indices
An input like x[c(1,4,5)] means give me the 1st, 4th, and 5th components of x. Another way to select components of a vector is with a list of TRUEs and FALSEs. x = c(_,_,_,_,_) y = c(TRUE,FALSE,FALSE,TRUE,TRUE) x[y] This is useful for selecting a subset of your data that meets some criterion examScore[heightInches > 72]
24
Strings A STRING is non-numeric information, like a label.
"glorp" x = "glorp" type x y = "yingle" c(x,y) We use strings for nominal variables x = c("male","male","female","female","male") You can't do much with string variables, but you can get truth values x == "male" examScore[sex=="male"] examScore[sex=="female"]
25
Scripts You can have R make you a new window where you can write notes and store code to run. [open a window] [write some code] [execute that code]
26
Today’s Assignment Pretend you have collected data on 5 subjects. For each subject, you have measured their height (in inches), sex, favorite color, distance they walk to school (in miles, including decimals), and time it takes them to get to school (in minutes). 1. Create variables for all five of these measures. Use realistic values.
27
Today’s Assignment 2. Compute the sum of heights of all your subjects.
3. Use your time-in-minutes variable to compute a new variable for time in hours. 4. Use your distance and time-in-hours variables to compute a new variable for average speed, in mph. Display the values of your new mph variable. 5. Have R display the heights of all your female subjects. 6. Have R display the walking time for all subjects that live more than one mile from school.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.