Lectures2-RExpressions - Writing Expressions in R 13 To...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Writing Expressions in R 13
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
To store a value for later, we can assign it to a variable . > x1 <- 32 %% 5 > print(x1) [1] 2 > x2 <- 32 %/% 5 > x2 # In interactive mode, this prints the object [1] 6 > ls() # List all my variables [1] "x1" "x2" > rm(x2) # Remove a variable > ls() [1] "x1" = ” and “ <- ” are both valid assignment operators in R. Choose one and use it consistently. As we’ll see “ == means something completely different. 14
Background image of page 2
Variable names must follow some rules: May not start with a digit or underscore (_) May contain numbers, characters, and some punctuation - period and underscore are ok, but most others are not Case-sensitive, so x and X are different Advice on variable names: Use meaningful names Avoid names that already have a meaning in R. If in doubt, check: > exists("pi") [1] TRUE 15
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A function is a portion of code that performs a speciFc task. Usually it takes some inputs, performs some computations, and returns a value. The inputs are called arguments to the function. When you use a function with a particular set of arguments, you are set to be calling the function. The computer evaluates the function call and returns the output. ±or now, we’ll work with R’s built-in functions, and the most important things to know are how to call the function and how to get help when you need it. 16
Background image of page 4
Task: Generate a sample of size 1000 from the normal distribution with mean 0 and variance 2. Verify graphically that the samples behave as you expect they should. Hint: Try help.search(“topic”) to look for functions that concern a particular topic. 17
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
R has a number of built-in data types. The three most basic types are numeric, character, and logical. You can check the type using the mode function. > mode(3.5) [1] "numeric" > mode("Hello") [1] "character" > mode(TRUE) [1] "logical" Actually, the three types are numeric, character, and logical vectors . There’s no such thing as a scalar in R, just a vector of length one. 18
Background image of page 6
A vector in R is a collection of values of the same type. You can join vectors together using the c (for “concatenate”) function. > # Creating new vectors > c(1.3, 2, 8/3) [1] 1.300000 2.000000 2.666667 > c("a", "l", "q") [1] "a" "l" "q" > c(TRUE, FALSE, FALSE) [1] TRUE FALSE FALSE > > # Concatenating existing vectors > x1 <- 1:4 > x2 <- 3:5 > c(x1, x2) [1] 1 2 3 4 3 4 5 19
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What do you think happens when you try to concatenate objects of different types? > c(1, 2, FALSE) [1] 1 2 0 > c(1, 2, "c") [1] "1" "2" "c" The last two expressions illustrate implicit coercion . You should try to avoid this in most situations. 20
Background image of page 8
The elements of a vector can have names . > unfair.coin <- c("heads" = 0.55, "tails" = 0.45)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/04/2011 for the course BUS 103 taught by Professor Opp during the Fall '10 term at University of California, Berkeley.

Page1 / 30

Lectures2-RExpressions - Writing Expressions in R 13 To...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online