R programming for beginners (GV900) ~ Solutions
Lesson 2: Practice exercises
Task 1
Open a new R script, write out a heading using comments; make sure to save the script somewhere you can access it later. Make sure you know where you have saved it.
Download, install and load “ggplot2” package into R; we’ll use it next week.
Solutions:
Task 2
a. small vector
Write code (in the R script, not in the console) to create a vector (using the combine function) called “small” that has numbers from 1 to 4, i.e., it has 4 values: 1, 2, 3 and 4.
Solutions:
b. big vector
Next, create another vector called “big” that has values from 5 to 8.
Solutions:
c. third vector
Now create a third vector called “sum” where you add small and big. Display the output of “sum.” What is the length of the vector called sum? Write the length in comments for yourself.
Solutions:
Task 3
a. first vector of numbers
Create a vector of numbers from 0 to 50 in increments of 5; store it as a variable called “first.”
Solutions:
b. second vector of numbers
Create a variable called “second” that is a vector of numbers from 5 to 60, which is of the same length as the first vector.
Solutions:
[1] 5.0 10.5 16.0 21.5 27.0 32.5 38.0 43.5 49.0 54.5 60.0
c. add vectors
Add the two vectors, naming the new vector “third.
Solutions:
Task 4
a. vector of names
Create a vector of six first names of politicians (real or hypothetical) and save them as a variable. Remember to name the variable.
Solutions:
b. sub vector
Save the third, fourth, fifth names as a separate variable.
Solutions:
Task 5
a. create a sample variable
Imagine we want to think about factors that affect whether someone turned out to vote in the last election or not. Call this variable ‘turnout’ and assume it is a nominal/categorical variable that can take on the value of ‘yes’ or ‘no’. Create a sample variable of length 100 called turnout that randomly takes on the given values such that approximately 80% of the total sample did turn out to vote.
Solutions:
Code
[1] "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "no" "no" "yes"
[13] "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "no"
[25] "yes" "yes" "yes" "yes" "yes" "yes" "no" "no" "yes" "yes" "yes" "no"
[37] "yes" "no" "yes" "yes" "yes" "yes" "no" "yes" "no" "yes" "yes" "yes"
[49] "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "no" "no"
[61] "no" "yes" "no" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes"
[73] "yes" "yes" "yes" "yes" "no" "yes" "yes" "yes" "yes" "yes" "yes" "yes"
[85] "no" "no" "yes" "no" "yes" "yes" "yes" "yes" "yes" "no" "yes" "yes"
[97] "yes" "yes" "yes" "yes"
turnout
no yes
18 82
[1] "no" "yes" "no" "yes" "no" "yes" "yes" "no" "yes" "yes" "no" "no"
[13] "yes" "yes" "yes" "no" "yes" "yes" "no" "yes" "yes" "yes" "yes" "no"
[25] "yes" "yes" "yes" "yes" "yes" "no" "no" "yes" "no" "no" "yes" "yes"
[37] "yes" "yes" "yes" "yes" "yes" "no" "yes" "yes" "no" "yes" "yes" "yes"
[49] "yes" "yes" "yes" "yes" "yes" "no" "yes" "yes" "yes" "yes" "no" "yes"
[61] "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes"
[73] "no" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "no" "yes" "no" "yes"
[85] "yes" "yes" "yes" "yes" "yes" "yes" "no" "yes" "yes" "yes" "yes" "yes"
[97] "yes" "yes" "yes" "yes"
turnout
no yes
21 79