3 Test Yourselves 1

Before working on this exercise, please read through the Getting Started and the Basics sections first, especially if you have no background in R.

Suggested answers are at the bottom of this page, but please do try the exercise before looking at them. :)

3.1 Instructions

  1. Download and install R.
  2. Download and install RStudio.
  3. Start an R Project.
  4. Open up a new script file in RStudio.
  5. Suppose we have the starting monthly salary of 10 men and 10 women (see Data section below). Enter the data provided into a data frame (call this data frame dataset) where one column represents the biological sex of the individual (call this column BioSex) and the other column represents the starting monthly salary (call this column Salary) of the individual. Make sure you have 20 rows (one for each individual) and 2 columns (one for each variable).
  6. Save the data frame into a .csv file. Name this file data.csv. Check in the working directory that the .csv file has been created correctly.

3.2 Data

Man Woman
3530 3790
4730 2720
4330 3170
3560 3320
4050 3190
4880 2890
4190 2920
3620 3390
3530 3790
5070 3680

3.3 Suggested Answers

# Alternative 1
dataset <- data.frame(BioSex = c("man", "man", "man", "man", "man", "man", "man", "man", "man", "man", "woman", "woman", "woman", "woman", "woman", "woman", "woman", "woman", "woman", "woman"), 
                 Salary = c(3530, 4730, 4330, 3560, 4050, 4880, 4190, 3620, 3530, 5070, 3790, 2720, 3170, 3320, 3190, 2890, 2920, 3390, 3790, 3680))

# Alternative 2
# In this alternative, I call the objects sex and money instead so that you can see how they work in data.frame function.

sex <- c(rep("man", 10),
         rep("woman", 10)) 
# tell R to replicate the string "man" 10 times and replicate the string "woman" 10 times. Then combine the results into a list and save it as sex. The first 10 values will be "man" and the next 10 will be "woman".

money <- c(3530, 4730, 4330, 3560, 4050, 4880, 4190, 3620, 3530, 5070, 3790, 2720, 3170, 3320, 3190, 2890, 2920, 3390, 3790, 3680)
# tell R to combine the 20 values into a list and save it as money

dataset <- data.frame(BioSex = sex, 
                      Salary = money) 
# create data frame

# Alternative 3
# Similar to Alternative 2 except that the three steps are combined into one command.

dataset <- data.frame(BioSex = c(rep("man", 10), 
                                 rep("woman", 10)), 
                      Salary = c(3530, 4730, 4330, 3560, 4050, 4880, 4190, 3620, 3530, 5070, 3790, 2720, 3170, 3320, 3190, 2890, 2920, 3390, 3790, 3680))

# Save data file
write.csv(dataset, "data.csv")