When you open RStudio, you should see something like this:
There should be 3 different windows along with a number of tabs.
This is the R console, where you key in commands to be run in an interactive fashion. Type in your command and hit the Enter key. Once you hit the Enter key, R executes your command and prints the result, if any.
ls()
.functionName
appears here when you type ?functionName
in the console.There isn’t anything here at the moment, but this space will become useful later when we are working with scripts. Click the icon in the top-left corner of the window, and click “R Script”. A new window pane that looks like a text editor opens up.
We’ll explore scripts later in the course, but for now, this is a useful place for us to type out long commands (especially those which span over multiple lines). To execute code from this window, highlight the code and click the button at the top of the window (or Cmd-Enter
on a Mac, Ctrl-Enter
on Windows).
You can use R has a high-powered calculator. For example,
1 + 2
## [1] 3
456 * 7
## [1] 3192
5 / 2
## [1] 2.5
Notice that the command 5/2
gave the result 2.5
, while several other programming languages would typically give 2
as a result.
There are several math functions which come with R. For example, to evaluate \(log (e^{25} - 2^{\sin(\pi)})\), we would type
log(exp(25) - 2^(sin(pi)))
## [1] 25
Apart from numbers, R supports a number of different “types” of variables. The most commonly used ones are numeric variables, character variables (i.e. strings), factor variables, and boolean (or logical) variables.
We can check the type of a variable by using the typeof
function:
typeof("1")
## [1] "character"
typeof(TRUE)
## [1] "logical"
We can change the type of a variable to type x
using the function as.x
. This process is called “coercion”. For example, the following code changes the number 6507232300
to the string "6507232300"
:
as.character(6507232300)
## [1] "6507232300"
typeof(6507232300)
## [1] "double"
typeof(as.character(6507232300))
## [1] "character"
We can also change variables to numbers or boolean variables.
as.numeric("123")
## [1] 123
as.logical(123)
## [1] TRUE
as.logical(0)
## [1] FALSE
Sometimes type conversion might not work:
as.numeric("def")
## Warning: NAs introduced by coercion
## [1] NA
Sometimes type conversion does not work as you might expect. Always check that the result is what you want!
as.logical("123")
## [1] NA
Often, we want to store the result of a computation so that we can use it later. R allows us to do this by variable assignment. Variable names must start with a letter and can only contain letters, numbers, _
and .
.
The following code assigns the value 2
to the variable x
:
x <- 2
Do not use the =
sign to assign values to variables! Although it works in R, it can cause a lot of confusion.
Notice that no output was printed. This is because the act of variable assignment doesn’t produce any output. If we want to see what x
contains, simply key its name into the console:
x
## [1] 2
For more complex objects that will encounter soon, we can use the str
function to get information on the internal structure of the object:
str(x)
## num 2
We can use x
in computations:
x^2 + 3*x
## [1] 10
We can also reassign x
to a different value:
x <- x^2
x
## [1] 4
What is the value of x
and y
after I execute the following code?
y <- x
x <- x^2
Let’s add a third variable:
z <- 3
Note that we now have 3 entries in our Environment tab. To remove an object/variable, use the rm()
function:
rm(x)
To remove more than one object, separate them by commas:
rm(y, z)
Let’s add the 3 variables back again:
x <- 1; y <- 2; z <- 3
To remove all objects at once, use the following code:
rm(list = ls())
This section is for documentation purposes: By displaying my session info, others who read this document will know what the system set-up was when I ran the commands above.
sessionInfo()
## R version 3.6.3 (2020-02-29)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 19.10
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.7.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] compiler_3.6.3 magrittr_1.5 tools_3.6.3 htmltools_0.3.6
## [5] yaml_2.2.0 Rcpp_1.0.1 stringi_1.4.3 rmarkdown_1.12
## [9] knitr_1.22 stringr_1.4.0 xfun_0.6 digest_0.6.18
## [13] evaluate_0.13