## R Basics

When you open R for the first time you get a confusing looking program. Below is a screenshot of what you get when you open R for the first time.

A brief tour of R: The blue circle is the console window; the yellow circle contains several different tabs, files, plots, packages, and the help panel are located here; the green circle contains the environmental and history tabs, it also contains a preview of your workspace and a shortcut on how to import data into R.

The console window is the place where R does actual work. While you can do your work in the console window, you are encouraged NOT to work within the console window.

Instead you should work in a script file! There are several reasons you should work in a script file, but the most important is that you can easily replicate and repeat your work. To open a script file, on the top of the console window click the “new” document tab in the upper left corner.

After opening a new script file, your R Studio will add a script window. When you type a new line in the script window, it will not immediately be executed.

Try some simple math in an R script file

``````3+2
3*2
3/2``````

To execute those lines, highlight the lines you want executed and click Run in the upper right corner of the script window. There is a keyboard shortcut for the Run button, Ctrl+Enter. If you’ve executed the lines you’ll notice the output is within the console window.

``## [1] 5``
``## [1] 6``
``## [1] 1.5``

You can program in R as well. You can assign values to variables, in the sense of computer science, by using the `<-` operator. For example, `x <- 5` assigns the value of 5 to `x`. While you can use `=` for assignment it is not recommended. Next, you can use the variable for mathematical purposes.

``````x = 5
x <- 5
x + 3``````
``## [1] 8``

Often it is easier to work in vectors, lists, and data frames. One way to create a vector is via a sequence. By typing `x <- 1:12` you are creating a sequence from 1 to 12, incrementing by 1, so the result is the vector

``##  [1]  1  2  3  4  5  6  7  8  9 10 11 12``

Another way to create a vector is element by element. For example, if we wanted to make a vector that contains the numbers `1 2 3 5 7 11 13` then we can do that via the assignment operation and the combine function `c()`

``````x <- c(1, 2, 3, 5, 7, 11, 13)
x``````
``## [1]  1  2  3  5  7 11 13``

Note that when working with vectors, be careful! In general, R makes assumptions! This means that it assumes the user knows what the output should be. For example, R can carry out mathematical operations on a vectors element-by-element in some situations.

``````y <- 7:1
x + y``````
``## [1]  8  8  8  9 10 13 14``
``x * y``
``## [1]  7 12 15 20 21 22 13``
``x + 2``
``## [1]  3  4  5  7  9 13 15``

Other times it will not carry out operations as you expect and in such situations R may return a warning or an error.

``x + c(1,2,3)``
``````## Warning in x + c(1, 2, 3): longer object length is not a multiple of
## shorter object length``````
``## [1]  2  4  6  6  9 14 14``

When you do encounter an error or a warning, you might need some help understanding what went wrong. All functions in R have a description by typing `?` in front of the function or `help()` with the name of the function you need help with and R will return the help file for the function. If you have trouble understanding the help file or an error/warning message, try google or stackexchange. Remember: The purpose of this class is not R! You’re expected to use R as a fancy calculator and when you get stuck, ask questions!