Learning goals

After completing these tasks you'll be able to follow and carry out the other tasks on this course. You will be able to run code from the console and script in RStudio, create variables, assign values to variables, use functions, load packages, save your script and construct simple loops. New concepts: variable, function, package, script, loop.

Exercises

1.1. How to use R

Demo 1: R and RStudio

Calculations

Demo 1: Calculations

Q1.1.1 Try to run the following commands on the command line (bottom left)

1+1

24/6

10-5-1

5-10/2

Q1.1.2 What do you notice about the order of the calculations? Can you use brackets to control the order of calculations as you would expect?

Q1.1.3 Now try to run some simple calculations from the script window (top left).

Variables

Demo 1: Variables

Q1.1.4 create a simple variable by running

x <- 1

Then only run

x

to see what the variable value is. What does the program output?

Tip: For some reason it's common to use <- for variable assignment in R, so we'll mainly use that. You can quickly type that in RStudio by pressing alt + - (windows) or option + - (mac). You can also use '=' for variable assignment if you'd prefer.

Q1.1.5 Try doing some arithmetic operations including x. For example x+5. Is the output what you would expect?

Q1.1.6 Try to assign an arithmetic operation to a variable called y. Does the variable save the operation or the end result of the operation?

Q1.1.7 Try adding together variables x and y, and assign the result to a variable called z.

Q1.1.8 If you just run z+10, this will not change the value of z. If you wanted to save the result of the calculation in z, how would you do that?

Functions

Demo 1: Functions

Q1.1.9 Run

a <- sum(1,2,3)

Q1.1.10 Identify function, argument and output (also called result) in the code in Q1.1.9

Tip: Keep in mind, that you need to add a comma between different arguments to a function. Otherwise you will get an error.

Q1.1.11 Is the output what you would expect?

Q1.1.12 Try running the function getwd(), which does not take any arguments. Can you guess what the output tells you?

Q1.1.13 Run ?getwd() to see if you were correct.

Tip: Remember that parentheses (and quotation marks) must always come in a pair. When you forget one quotation mark or parenthesis (which happens to all of us at some point), R tells you it's expecting more input by showing "+" on the console. If this happens, you can just type in the missing symbol and press enter. To cancel out what you've typed, press the escape key on Windows or Ctrl-C on OS X or Linux.

Saving your script

Demo 1: Errors and saving

Q1.1.14 Make a folder on your computer for this course. Save the script file (.R) you have been working on in that folder.

1.2. Simple data types

Demo 1: Data types (lecture)

Demo 1: Data types (demo)

Numbers ("numeric")

Q1.2.1 The function typeof() takes one argument, which is the object whose type (class) you want to find out. Use the function to check the class of the following:

x

5.1

pi

2L

Characters (text)

Q 1.2.2 Use the same function as above to find out the class of

'abc'

Tip: To separate character values from variable names, characters are specified in R by putting them between '' (single quotes) or "" (double quotes).

Q1.2.3 Try running

s <- 'abc'
s+5

Why do you think this does not work? (Hint: it has to do with data types.)

Tip: When you want to run multiple lines of code, you can select them with your mouse and then press 'run' (or the shortcut keys)

Logical (Boolean, i.e. TRUE or FALSE)

Q1.2.4 Use your variables x, y and z and logical operators greater than (>), less than (<) and equals (==) to make a logical statement which will evaluate to (output) FALSE and another statement which evaluates to TRUE.

Tip: Since "=" is used to assign variables, in programming you need to use "==" (two equals signs) to check whether two things are equal. Other logical operators include != (is not equal to), <= (less than or equal to), >= (greater than or equal to)

Vectors

Q1.2.5 Run

vec <- 1:10

What is the output? What type is the output?

Q1.2.6 Make the following two vectors

vector1 <- seq(1,20,by=5)
vector2 <- seq(1,20,length=5)

Are they the same? What did the arguments by and length do?

Hint: you can use ?seq to get the help for the function

Q1.2.7 Using function seq, make a vector that goes from 40 to 100 in steps of 10 and assign it to variable called vector3.

Q1.2.8 Create a vector of characters a,b,c,and d by using the function c() (c is short for combine). Remember to use quotation marks for the characters.

Vector indexing

Q1.2.9 Use the square bracket notation to access the second element of vector3.

Tip: If you have programmed before, you may be used to indexing which begins at 0. Keep in mind that R starts indexing at 1.

Q1.2.10 See if you can get R to return (output) the first and second element of vector1 using only one command

Q1.2.11 Try what happens if you run

vector1[c(TRUE, FALSE, TRUE, FALSE)]?

Q1.2.12 What about

vector1>10?

Q1.2.13 Try to retrieve values larger than 50 but smaller than 80 from vector3 using logical indexing.

Hint: you can combine logical operators with & (and) and | (or)

1.3 Loops

Demo 1: Loops

Q1.3.1 Try running the following simple for loop

for(i in 2010:2020){
  print(paste("Is this year ",i,"?", sep=''))
}

Q1.3.2 Modify the loop above to go from 2020 to 2025

Q1.3.3 Make a loop which sums together values from 1 to 100 and in the end, prints out the result. Make sure you got the correct answer by checking your result with sum(). (This can be a fairly difficult task if you do not have any programming experience.)

Hint: If you get stuck, there's no shame in googling to get ideas how to accomplish something you don't know how to do in R!

1.4 Packages

Demo 1: Packages

Q1.4.1 Install package 'psych'

Q1.4.2 Load the package with library

Q1.4.3 Try to run the following command from the psych package: describe(vector2). If you run the command without errors and get a list of descriptive statistics (mean, sd, median etc), it means you have successfully installed psych

 

Created by Juulia Suvilehto
juulia.suvilehto@iki.fi

Creative Commons License