Welcome

Welcome

Welcome to this course on R, a programming language for statistics and data modeling!

To warm up, Let's calculate the average temperature of two days.

> (88 + 72) / 2

//Output Below

[1] 80

Exactly! At it's core, we can use R as a calculator.

Why R?

Seems like we can just use a simple calculator instead of R, doesn't it? But what if you love barbecues? Could R help you plan your BBQ's?

How would you calculate the average weekend temperature throughout an entire year?

  • Use R to find the result

Yup! R can do all of that in a jiffy. We'll find out more about how R can help us plan our BBQ's after we've learned a few of the basics.

Input

When using R on a computer, we'll need to use a terminal. At the very beginning of the command line you'll always see the >.

> "Hello there!"

//Output Below

[1] Hello there!

That sign is a prompt that lets us know that R expects a command from us.

Output

R is made for large collections of numbers and can sometimes get confusing to read. Luckily, R helps us out when providing output.

What might be the correct order for this sequence of numbers?

[1]  5  6  7  8  9 10 11 12
[9] 13 14 15 16 17 18 19 20
[17] 21 22 23 24 25

R prints out the index of the first number in each line:13 is the 9th number in our collection and 21 is the 17th. We'll see [1] a lot in our results.

Assigning values

As we know, programming languages often use variables to store different values.

Let's guess how variables are assigned in R.

> variable <- 5

Exactly! We can read that as variablegets5.

Psst: once something is created in R, it's called an object.

Operators

We can also use arithmetic operators with variables. To display the contents of a variable, we just need to write it out in the prompt.

Let's add the two variables.

> var_1 <- 9
> var_2 <- 3
> result <- var_1 + var_2
> result

//Output Below

[1] 12

Wahey! We can perform basic arithmetic operations with +, -, *, and /. We printed out the sum by simply typing in result.

Data structures

However, R was designed for collections of data, not simple numbers.

To combine different values into a collection, we can use a function called c().

> var <- c(0,1,1,2,3)
> var

//Output Below

[1] 0 1 1 2 3

Awesome! We just created a collection of data and assigned it to var.

Data structures II

Which of these do you think are collections in R.

  • var <- 3
  • var <- c(5,-3,27)

Yup! R treats everything as a collection. Single values are collections with just 1 element.

Atomic vectors

These collections have a special name in R: atomic vectors. They can also be made out of more than just numbers.

Let's use a function called class to check out other atomic vectors.

> a <- c(1,2)
> b <- c("a","bee","cdefg")
> c <- c(TRUE,FALSE)
> class(a)
> class(b)
> class(c)

//Output Below

[1] "numeric"
[1] "character"
[1] "logical"

Great! a is a numeric , b is a character, and c is a logical vector.

Psst: atomic means that it's the smallest component of something bigger.

Other structures

Besides atomic vectors, data can be stored in many other ways in R: matrices, factors, data frames, and lists.

Why would we need so many different structures?

  • To define relationships between data
  • To get better results
  • We don't, they are all basically the same

Yup! Data comes in all shapes and sizes we need to store it in understandable ways.

Functions

Just like in other languages, we can also make our own functions in R. These are sets of instructions we can reuse as often as we want.

function_name <- function(x,y){
  return x * y
}
function_name(2,4)

//Output Below

[1] 8

We call a function by typing its name folowed by (). Here, we're taking 2 variables as input, multiplying them and displaying the result.

Comments

In R, we can use # to comment out parts of our code.

> # comments begin with a '#'
> # comments are ignored by the program
> a = 2
> # a = 3
> a

//Output Below

[1] 2

See how the line # a = 3 was ignored? You can use comments to write notes for yourself or explanations for others.