Coding Basics
- You can do basic math calculations in R:
1 / 200 * 30
## [1] 0.15
- You can create new objects in the assignment operator:
x <- 3 * 4
print(x)
## [1] 12
- You can combine multiple elements into a vector:
primes <- c(2, 3, 5, 7, 11, 13)
print(primes)
## [1] 2 3 5 7 11 13
- You can do basic arithmetic on elements in created vectors:
primes * 2
## [1] 4 6 10 14 22 26
- KEYBOARD SHORTCUT for “<-” in the console = “alt” + “-” (FYI: alt
= “option” on MacBooks)… notice that it will add a space before and
after “<-”, yay efficiency!
What’s in a name?
- If you want to look up all the names that start with a certain
word/letter, type that word/letter and hit “command” + “alt” + “up
arrow”
Calling Functions
If you want to look up functions, you can type in the console and
hit “tab”, for example to look at sequence functions type “se” in the
console and hit tab. A drop-down menu will appear with every function
beginning with “se”.
Sequences example:
seq(from = 1, to = 10)
## [1] 1 2 3 4 5 6 7 8 9 10
- Functions can also be simplified (look at the difference in code
between this and above):
seq(1, 10)
## [1] 1 2 3 4 5 6 7 8 9 10
END OF CHAPTER 2.
Comments