R is a statistical computing environment. In it's most basic form, it runs as a command-line tool. This means you can open up a terminal (a text-only interface to your computer's operating system) and interact with R by typing commands and seeing the result. We'll actually do this, just once, to make sure R is installed properly.
While command-line tools are actually very powerful, they are not a very user-friendly way to use software. So as well as installing R, we'll also install a program called RStudio, which is an Integrated Development Environment (IDE). This provides a more user-friendly approach to using the underlying R environment. Note you'll still need to type commands and see the results, but we'll have a better environment to do that in
pkg file for the latest version.Applications in Finder, or by finding the R app in LaunchPad
>
symbol is a prompt; it is an indication that you're expected to type something.
R will read what you type, evaluate it, and print the result. It
will do this in a loop, i.e. repeatedly.
6 * 7
at the prompt. R will read this expression, evaluate it, and print the result.
c() (c stands for
"combine") function. Enter the following:
c(2,3,5,7,8)
This isn't very exciting: R simply reads the expression, evaluates it to a vector
containing the five values, and prints those five values.
mean( c(2,3,5,7,8) )
Congratulations: you just did your first statistics with R! Admittedly, this wasn't
too exciting, but it's a start.
Using the console on its own is not very user friendly. Here, we'll install RStudio, which will make things easier going forward. Note we'll still be typing commands, but RStudio will give us lots of useful functionality for managing those commands.
x <- c(2,3,5,7,8)
(The thing that looks like an arrow here is just a "less than sign", <, followed by
a minus sign, -).
c(2,3,5,7,8) and save it in an object called x.
x
is displayed there, with its current value.
x using the mean
function:
mean(x)
x, using the
sd function.
boxplot(x)
R supports the idea of packages, which are libraries of code designed to provide functionality beyond the base functionality of R. There are thousands of R libraries that have been written for many different fields of scientific research and data analysis.
To use a package, it must
We will install a package called tidyverse, which we'll use regularly throughout the course.
To test the installation, try to load the library. You can do this by typing
library(tidyverse)
in the console.
If it's successful, you should see output looking like
── Attaching packages ────────────────────────────────────────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6 ✔ purrr 0.3.4
✔ tibble 3.1.8 ✔ dplyr 1.0.9
✔ tidyr 1.2.0 ✔ stringr 1.4.0
✔ readr 2.1.2 ✔ forcats 0.5.1
── Conflicts ───────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
If you've made it this far, you've successfully installed R and RStudio, done some very simple statistics with R, and created your first simple plot, as well as installing an R package. Next time, we'll learn a little bit more about how R works and start to explore some data.