R Syntax
Run the commands above in the RStudio console.
How many decimals does R display by default?
White space is generally ignored in R (but helpful for readability).
Everything in R is case sensitive.
The # character tells R that what comes after is a comment.
A key technique in all programming languages is to store values in the form of variables.
In R, you can use the ‘=’ or ‘<-’ operators to do this.
RStudio keyboard shortcut to insert ‘<-’: alt + -
The term ‘variable’ and ‘object’ are generally synonymous.
Note when you assign a value to a variable, R doesn’t print the result to the console. To see the value of a variable, simply type it by itself.
Pretty much anything can be saved to a variable. Here we save some text and dates.
## [1] "vanilla"
## [1] "2021-07-21"
You can give a new value to a variable at any time.
You can use ‘current’ value as part of the expression for the ‘new’ value.
The rules for naming variables are pretty flexible. You can use numbers, letters, and most special characters.
Rules to take note of:
There are a handful of popular naming styles. Pick one that you like, and be consistent!
Style | Example |
---|---|
alllowercase | adjustcolor |
period.separated | shoe.size |
underscore_separated (aka snake case) | numeric_version |
lowerCamelCase | addTaskCallback |
UpperCamelCase | SignatureMethod |
All variables have a class or data type, which you can view using class().
## [1] "numeric"
Other common data types:
Most variables are vectors, in other words they can store multiple values. For basic data types, like numbers and characters, these are called vectors.
When we enter x <- 1, we are actually creating a numeric vector of length 1.
## [1] 24
rep() is a function that repeats a value, returning a vector:
## [1] "ball" "ball" "ball" "ball" "ball"
rnorm() is a function that can generate multiple random numbers:
## [1] -0.01511176 0.80493927 -0.14100897 1.28987350 1.51903961
c() stands for combine. It returns a vector containing the arguments passed (which all have to be the same type).
## [1] TRUE FALSE TRUE
LETTERS is an example of a built-in constant. It contains a vector of length 26 containing characters:
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
The [1] and [18] in the output tells you the index of the element displayed after it.
## [1] 26
To extract a single element from a vector, use square bracket notation. Inside the square brackets, put the index of the element you want.
## [1] "B"
To return multiple elements, pass multiple indices.
## [1] "B" "C" "D"
## [1] "B" "C" "D"
You can also use square brackets to extract elements in a different order.
## [1] "D" "C" "B"
Many functions take arguments. Arguments can be required or optional.
See the function’s help page to determine which arguments are expected.
If you pass arguments to a function in the order expected and don’t skip any, you don’t have to name them:
rnorm(500, 50, 3) ## generate 500 normally distributed random
## numbers with a mean of 50 and stand dev = 3.
If you don’t pass arguments in expected order (or skip some), you have to name them in the parentheses: