Basic R

Milica Cudina

2020-08-31

Inputting commands into R

You can input commands into R through …

  • … the console
  • … a script
  • … an R-notebook

The console

Let start by opening our console in RStudio.
How would we do the following:

  • Do simple arithmetic: add, subtract, multiply, divide?
  • Assign a value to a variable?
  • Take the natural logarithm of 10?
  • Exponentiate 2?
  • Take the square root of 4?
  • Take the third root of 27?

The console (cont’d)

  • If you define a variable in a command in the console, it will remain available in all the subsequent commands (look at the environment in the upper right)
    1. Assign the value 7 to variable x
    2. Assign the value 9 to variable y
    3. Define z to be the sum of x and y
    4. Print out the value of the square of z
  • This is the perfect place to ask for help

Vectors

  • We already know how to assign values to variables
  • How do we create vectors?
  • Use the concatenation operator c
  • Let’s go into the console and play with it

The concatenation operator

  • Create a vector x with values 1,4,9
  • Create a vector y with values 1,8,27
  • Concatenate vectors x and y into vector z and print it out
  • Create a vector p of names of Generation I starters
  • Print out your vector p

Vectors of equally spaced numbers

  • We use the colon : to make vectors of consecutive numbers
  • See what happens when you run 1:10
  • See what happens when you run -10:0
  • What if you want to list numbers in increments of 5?
  • Seek help for command seq
  • Now, create a list of numbers from 1 to 25 increasing by 4

Functions and operations on vectors

  • Vectorization: if you give a function a vector (or vectors) as an argument, the returned value will be a vector of results of that operation performed element by element
  • Print out the list of cubes of the first consecutive 5 natural numbers

More functions and operations on vectors

  • What if you want to multiply two vectors?
  • Create a vector q of 1,2,3
  • Create a vector e of -1,1,2
  • Add q and e; what do you get?
  • Multiply q by e; what do you get?

Recycling

  • If you try to add up or multiply vectors of different lengths, the shorter one gets “recycled”
  • This is useful when the short vector is of length 1
  • Print out the list of the first 12 even numbers
  • Pick your favorite 4 non-zero digits; put them in a vector t
  • Pick your other favorite 4 digits; put them in a vector o
  • Print out a vector of 4 numbers with the tens digits from t and the ones digits from o (paired up element by element)

Extracting parts of vectors

  • What if you just want a part of a vector?
  • Then you use the indexing operator []
  • Let’s practice what it does
  • Define the vector x as the first 5 natural numbers
  • How do you extract the first element in x?
  • How about the second and the fourth elements in x?
  • How about all but the third element in x?
  • What if you want to repeat elements?

Extracting parts of vectors

  • Caveat #1: Counting starts from 1
  • Caveat #2: Negative numbers mean complementation, not counting from the back
  • Caveat #3: The contents of [] must be a vector itself; a nice consequence is that this means that we can input conditions for whether we keep an element or not

Scripts

  • What if you do not want to use R simply as a calculator?

  • Then you use scripts: text files that hold R code (with extension .R)

  • You can run scripts as a whole or just parts by highlighting

  • Scripts can be saved

Creating scripts

  • Now, go to File->New File->R Script

  • A new pane in the upper left corner, just above the console should appear: that’s your script editor

  • Now, let’s write a script that calculates the sum of the first 25 odd numbers

Running scripts

  • If you want to run a single line, go to it and click Run
  • If you want to run an interval of lines, highlight them and click Run
  • If you want to run the whole script, click Source
  • Your output will appear in the console

R Notebooks

  • These are text files containing chunks of R code mixed with regular text
  • Let’s try to see what happens if we create one
  • Go to File->New File->R Notebook