R is a programming language focused on statistics, data visualization, and data analysis. It is open source, which means there is a rich trove of libraries and add-ons constantly being developed by the open source community.
R is free to download and use. Follow the links below to download R if you would like to try it.
- Linux Binaries
- RStudio – Not required, but anyone looking for a better developed environment may want to check it out. I will be using just the base install of R for the following lessons though
When R first starts up, this is what you will see. I am not going to focus too much on a grand tour, as most of the menus in R are pretty self explanatory. Instead, let’s jump right into the coding. Move your cursor to where my big red arrow is:
There is an unwritten rule that states the first line of code you need to learn in any language is Hello World. Well, I am not going to do that. R is a stats program. Why don’t we start with some numbers instead.
As you can see, R uses your standard arithmetic operations (+,-,*,/,^)
Assigning variables in R is easy. “<-” is the designated syntax used to assign a variable. One great thing about R is that you do not need to declare variables in advance. R assigns the data type based on the input you give the variable.
The main data types in R are:
- Numeric: 1, 2.33, etc
- Integer: 2L
- Logical: TRUE, FALSE
- Character: string
- Complex: 2+3i (remember those from Trig class)
Vectors allow you group multiple elements under one name. Use the syntax <-c() when creating a vector
Lists allow you to group unlike items – even vectors and strings: