# Store the value 2 under the name x
x <- 2
# Typing the name on its own prints the value
x[1] 2
Statistical Methods with R
Unit A · Chapter 02 · Lecture 02b
Developed by Jeffrey M. Girard
Assignment
Naming
Functions
Vectors
Form
name <- value
Example
office <- 7858640841
<- stores a value under a name. Assigning prints nothing; typing the name on its own is how you ask R to show you what it holds.
[1] 8
[1] 5.5
Wherever a value could go, a name holding that value can go instead, as often as you need it.
Using an object never changes it. To update one, assign the result back to the same name.
[1] 13
[1] 4.333333
The right-hand side of <- is just an expression, so it can use any objects that already exist. That is how a script builds up from one line to the next.
a-Z0-9_.age ≠ Age)A good name is the shortest one a reader can still understand six months later.
Error: unexpected symbol in "heart rate"
A space stops R mid-line. A dash is worse: R reads heart-rate as heart minus rate, so it fails looking for an object rather than telling you the name is illegal. Use an underscore: heart_rate.
@ already means something else in R, so it reads age@time2 as an operation on an object called age. Letters, numbers, _ and . are all a name may hold.
Error: unexpected input in "1_"
Error: unexpected symbol in "_heart_rate"
Move the digit to the end and the name is fine.
[1] 93
[1] 88
One capital letter makes an entirely different object. R will not warn you, because it has no way to know you meant the other one.
Form
output <- fn(input)
Example
out <- f(in1, in2)
output <- function_name(input)
A function gives a name to something you would otherwise have to spell out. It is easier to write, and far easier to read back.
[1] 3
[1] 1
Anything that produces a value can go in the input slot: a literal, an object, or another calculation. R works out the inside first, then hands the result over.
output <- function_name(argument, argument_name = argument_value)
[1] 0.67
[1] 1
[1] 1
Extra arguments change how a function does its job. Most have a default, which is what you get when you leave them out.
Form
name <- c(value, value)
Example
v <- c(1, 2, 3)
vector_name <- c(element1, element2, element3)
Error: unexpected numeric constant in "x <- 4 9"
Elements must be separated by commas and wrapped in c(). Without it R sees several values where it expected one.
[1] 4 9 16 25 2 3
[1] 4 9 16 25 2 3 20
c() flattens whatever you give it into one vector, so there is no such thing as a vector nested inside another.
[1] 5 10 17 26
[1] 12 27 48 75
[1] 4 9 16 25
An operation applies to every element at once, with no loop to write. As with a single value, the result is not saved unless you assign it.
These return a vector as long as the input: one answer per element.
These collapse the whole vector to a single number. Note the last one: R names this function mean(), and a reasonable guess is still an error.
[1] NA
[1] 12.5
NA means “we do not know”, so any answer depending on it is also unknown. That is a feature, not a bug. na.rm = TRUE answers from what you have.