R programming for beginners (GV900) ~ Solutions

Lesson 2: Practice exercises

Homework
Author
Published

Sunday, December 24, 2023

Modified

Saturday, January 6, 2024


Task 1

  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.

  2. Download, install and load “ggplot2” package into R; we’ll use it next week.

Solutions:

Code
# install.packages("ggplot2")
# library(ggplot2)

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:

Code
small <- c(1:4)
small
[1] 1 2 3 4

b. big vector

Next, create another vector called “big” that has values from 5 to 8.

Solutions:

Code
big <- c(5:8)
big
[1] 5 6 7 8

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:

Code
sum <- small + big
sum # length: 4
[1]  6  8 10 12
Code
length(sum)
[1] 4

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:

Code
first <- seq(from = 0, to = 50, by = 5) 
first
 [1]  0  5 10 15 20 25 30 35 40 45 50
Code
first <- seq(from = 0, to = 50, by = 5)
first
 [1]  0  5 10 15 20 25 30 35 40 45 50

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:

Code
second <- seq(from = 5, to = 60, length.out = 11)
second
 [1]  5.0 10.5 16.0 21.5 27.0 32.5 38.0 43.5 49.0 54.5 60.0
Code
second <- seq(from = 5,
              to = 60,
              length.out = length(first))
second
 [1]  5.0 10.5 16.0 21.5 27.0 32.5 38.0 43.5 49.0 54.5 60.0
Code
length(first)
[1] 11
Code
length(second)
[1] 11

c. add vectors

Add the two vectors, naming the new vector “third.

Solutions:

Code
third <- first + second
third
 [1]   5.0  15.5  26.0  36.5  47.0  57.5  68.0  78.5  89.0  99.5 110.0
Code
third <- first + second
third
 [1]   5.0  15.5  26.0  36.5  47.0  57.5  68.0  78.5  89.0  99.5 110.0

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:

Code
politicians <- c("Amelia", "Ben", "Chris", "Dan", "Ellen", "Fiona")
politicians
[1] "Amelia" "Ben"    "Chris"  "Dan"    "Ellen"  "Fiona" 

b. sub vector

Save the third, fourth, fifth names as a separate variable.

Solutions:

Code
sub_politicians <- politicians[3:5]
sub_politicians
[1] "Chris" "Dan"   "Ellen"

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
set.seed(127) # make sure we get the same outcome each time.
turnout <- sample(x = c("yes","no"), 
                  size = 100, 
                  replace = TRUE,
                  prob = c(0.8,0.2))
turnout
  [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"
Code
table(turnout)
turnout
 no yes 
 18  82 
Code
turnout <- sample(
    x = c("yes", "no"),
    size = 100,
    prob = c(0.8, 0.2),
    replace = TRUE
  )

turnout
  [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"
Code
table(turnout)
turnout
 no yes 
 21  79 
Back to top