{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lectures2-RExpressions

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

This preview shows pages 1–9. Sign up to view the full content.

Writing Expressions in R 13

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

View Full Document
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
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

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

View Full Document
A function is a portion of code that performs a specific 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. For 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
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

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

View Full Document
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
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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 30

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

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

View Full Document
Ask a homework question - tutors are online