Vectors

Question 1

The built-in vector letters contains the letters of the English alphabet. Use an indexing vector of integers to extract the letters that spell ‘cat’.

cat <- letters[c(3, 1, 20)]

Question 2

The function colors() returns all of the color names that R is aware of. What is the length of the vector returned by this function? (Use code to find the answer.)

col_length <- length(colors())

Question 3

The function call runif(1000, 0, 1) will draw 1000 numbers from a uniform distribution from 0 to 1, which simulates the p-values that you would get from 1000 experiments where the null hypothesis is true. Store the result of this call in pvals. Create a logical vector called is_sig that is TRUE if the corresponding element of pvals is less than .05, FALSE otherwise (hint: vectorized operations from the last lession), then use this logical vector to pull out those p-values. Finally, calculate the proportion of those p-values that were significant.

pvals <- runif(1000, 0, 1)

is_sig <- pvals < .05

prop_sig <- length(pvals[is_sig]) / length(pvals)

# alternatively:
prop_sig <- mean(is_sig)
prop_sig <- mean(pvals < .05)

Tabular Data

Question 4

Create a tibble with the columns name, age, and country of origin for 3 people you know.

# you can do this with the tibble function
people <- tibble(name = c("Lisa", "Ben", "Robbie"),
                 age = c(42, 43, 11),
                 country = c("US", "UK", "UK") )

# also note:
# you can type this in row by row, rather than column by column,
# using the 'tribble' function
people <- tribble(~name,  ~age, ~country,
                  "Lisa",   42,  "US",
                  "Ben",    43,  "UK",
                  "Robbie", 11,  "UK")

Question 5

Convert the built-in base R iris dataset to a tibble, and store it in the variable iris2.

iris2 <- as_tibble(iris)

Question 6

Create a tibble that has the structure of the table below, using the minimum typing possible. (Hint: rep()). Store it in the variable my_tbl.

ID A B C
1 A1 B1 C1
2 A1 B2 C1
3 A1 B1 C1
4 A1 B2 C1
5 A2 B1 C1
6 A2 B2 C1
7 A2 B1 C1
8 A2 B2 C1
my_tbl <- tibble(ID = 1:8,
                 A = rep(c("A1", "A2"), each = 4),
                 B = rep(c("B1", "B2"), 4),
                 C = "C1")    

Data Import

Question 7

Download the dataset disgust_scores.csv and read it into a table.

# change to the location to where you put your csv file
disgust <- read_csv("https://psyteachr.github.io/msc-data-skills/data/disgust_scores.csv")

Question 8

Override the default column specifications to skip the id column.

my_cols <- cols(
  id = col_skip()
)

disgust_skip <- read_csv("https://psyteachr.github.io/msc-data-skills/data/disgust_scores.csv", col_types = my_cols)

Question 9

How many rows and columns are in the dataset from question 7?

## dim() returns a vector c(rows, cols)
dimensions <- dim(disgust)
disgust_rows <- dimensions[1]
disgust_cols <- dimensions[2]

## nrow() returns the number of rows
disgust_rows <- nrow(disgust)

## ncol() returns the number of columns
disgust_cols <- ncol(disgust)

Answer Checks

You’ve made it to the end. Make sure you are able to knit this document to HTML. You can check your answers below in the knit document.

Question Answer
Question 1 correct
Question 2 correct
Question 3 correct
Question 4 correct
Question 5 correct
Question 6 correct
Question 7 correct
Question 8 correct
Question 9 correct