This document contains the solutions to the exercises from the practical session of Day 1 of the Introduction to R workshop. The session focuses on the fundamentals of working with data in R, including data import and export, understanding and handling different data types, and basic data manipulation techniques. The exercises provide hands-on practice with these core concepts and help build a foundation for efficient data analysis and programming in R.

1. Create a new folder and call it DAY 1
- Add the data files Data.xlsx, Data.txt, Data.csv, origin.txt to this folder
- Set your working directory to the same folder and check whether it’s ok.

# Note that the path to your folder will be different

setwd("~/ADS Courses/Intro to R/DAY 1")

getwd()
#> [1] "C:/Users/aagten/Documents/ADS Courses/Intro to R/DAY 1"

2. Open the Data.csv CSV file
The data represents the results of a statistics course. Twelve people completed one of three workshops, took an exam and then filled out the evaluation form. The 3 last variables refer to the answer they gave on 3 of the questions (scale 1 to 5).

# Read the dataset
Data <- read.csv("Data.csv",header=T)

# Look at the dataset
View(Data)

# Names of the variables are stored as a distinct part of the data frame
names(Data)
#> [1] "...1"      "workshop"  "gender"    "exam"      "question1" "question2"
#> [7] "question3"

# The dimensions of a table can be extracted using the dim() function  
dim(Data)
#> [1] 12  7

# The number of rows and columns
nrow(Data)
#> [1] 12
ncol(Data)
#> [1] 7

# Extract the variable 'workshop'
Data$workshop
#>  [1] 1 2 3 1 2 3 1 2 3 1 2 3

3. Data types
Objects of class data.frame represent data the traditional table-oriented way. Each row consists of a single observation and the variables are arranged into columns. The variables can be of different types. A more comprehensive overview of the current data structure is given by the str() function.

# Get the structure of the dataset
str(Data)
#> 'data.frame':    12 obs. of  7 variables:
#>  $ ...1     : chr  "Peeters" "Bruyneel" "Pasmans" "Van den Broeck" ...
#>  $ workshop : int  1 2 3 1 2 3 1 2 3 1 ...
#>  $ gender   : chr  "female" "female" "female" "female" ...
#>  $ exam     : num  9 16.5 14 12 NA 10 14.5 15 9 8 ...
#>  $ question1: int  1 2 2 2 3 2 4 5 NA 5 ...
#>  $ question2: int  1 1 4 2 1 3 5 4 3 3 ...
#>  $ question3: int  5 4 3 4 NA 3 2 5 2 4 ...
  • Check the class of the variable ‘exam’, which is part of the ‘Data’ object      
  • Check the class of the variable ‘gender’, which is part of the ‘Data’ object      
  • In order to do further statistical analysis, we need to convert ‘gender’ to a factor.    
  • Check the variable ‘workshop’. Try to convert it to a factor with three levels: 1=R, 2=SAS, 3=SPSS.     
  • Change the name of the unidentified column into “id”   
# Check the class of exam - numeric variable
class(Data$exam)
#> [1] "numeric"

# Check the class of gender - character variable
class(Data$gender)
#> [1] "character"

# Set gender to factor variable
Data$gender <- as.factor(Data$gender)

# Check whether gender was correctly specified as being a factor variable
is.factor(Data$gender)
#> [1] TRUE

# Convert workshop to a factor with three levels: 1=R, 2=SAS, 3=SPSS. 
Data$workshop <- as.factor(Data$workshop)
levels(Data$workshop) <- c("R","SAS","SPSS")
Data$workshop
#>  [1] R    SAS  SPSS R    SAS  SPSS R    SAS  SPSS R    SAS  SPSS
#> Levels: R SAS SPSS

# Change the name of the unidentified column into “id” 
colnames(Data)
#> [1] "...1"      "workshop"  "gender"    "exam"      "question1" "question2"
#> [7] "question3"
colnames(Data)[1] <- "id"
colnames(Data)
#> [1] "id"        "workshop"  "gender"    "exam"      "question1" "question2"
#> [7] "question3"

4. Making a new variable
- Make a new variable called ‘total’ that computes the total result of the 3 questions
- Try to change the name of the variable ‘gender’ into ‘sex’ using the function names()

# New variable that totals the results of the three questions
Data$total <- Data$question1+Data$question2+Data$question3 
head(Data)
#>               id workshop gender exam question1 question2 question3 total
#> 1        Peeters        R female  9.0         1         1         5     7
#> 2       Bruyneel      SAS female 16.5         2         1         4     7
#> 3        Pasmans     SPSS female 14.0         2         4         3     9
#> 4 Van den Broeck        R female 12.0         2         2         4     8
#> 5     Verbrugghe      SAS female   NA         3         1        NA    NA
#> 6         Steppe     SPSS female 10.0         2         3         3     8

# Rename variable gender
names(Data)
#> [1] "id"        "workshop"  "gender"    "exam"      "question1" "question2"
#> [7] "question3" "total"
names(Data)[3]<- "sex"
names(Data)
#> [1] "id"        "workshop"  "sex"       "exam"      "question1" "question2"
#> [7] "question3" "total"

5. Exporting a modified table
Export the “Data’ object to a .csv file and open the file in excel to check whether it was exported correctly.

write.csv(Data, "MyModifiedData.csv")

6. Sorting
Your dataframe is a two-dimensional table. Elements, rows and columns can be selected the same was as in the matrix example in the slides.

# Selecting the first row of the dataset
Data[1,]  
#>        id workshop    sex exam question1 question2 question3 total
#> 1 Peeters        R female    9         1         1         5     7

# Selecting the first 5 rows of the dataset
Data[1:5,] 
#>               id workshop    sex exam question1 question2 question3 total
#> 1        Peeters        R female  9.0         1         1         5     7
#> 2       Bruyneel      SAS female 16.5         2         1         4     7
#> 3        Pasmans     SPSS female 14.0         2         4         3     9
#> 4 Van den Broeck        R female 12.0         2         2         4     8
#> 5     Verbrugghe      SAS female   NA         3         1        NA    NA

# Selecting column 1 and column 3 from the dataset
Data[, c(1,3)]
#>                id    sex
#> 1         Peeters female
#> 2        Bruyneel female
#> 3         Pasmans female
#> 4  Van den Broeck female
#> 5      Verbrugghe female
#> 6          Steppe female
#> 7          Jansen   male
#> 8           Bosch   male
#> 9       Verlinden   male
#> 10      De Pooter   male
#> 11         Maenen   male
#> 12         Saenen   male

# Selecting column 1 from the dataset
Data[,1]
#>  [1] "Peeters"        "Bruyneel"       "Pasmans"        "Van den Broeck"
#>  [5] "Verbrugghe"     "Steppe"         "Jansen"         "Bosch"         
#>  [9] "Verlinden"      "De Pooter"      "Maenen"         "Saenen"

# Selecting the first five variables from the dataset
Data[,1:5]
#>                id workshop    sex exam question1
#> 1         Peeters        R female  9.0         1
#> 2        Bruyneel      SAS female 16.5         2
#> 3         Pasmans     SPSS female 14.0         2
#> 4  Van den Broeck        R female 12.0         2
#> 5      Verbrugghe      SAS female   NA         3
#> 6          Steppe     SPSS female 10.0         2
#> 7          Jansen        R   male 14.5         4
#> 8           Bosch      SAS   male 15.0         5
#> 9       Verlinden     SPSS   male  9.0        NA
#> 10      De Pooter        R   male  8.0         5
#> 11         Maenen      SAS   male 16.0         4
#> 12         Saenen     SPSS   male 15.0         4

# Selecting the first observation of the first variable
Data[1,1]
#> [1] "Peeters"

The indexing offers the possibility to switch the position of the columns and rows. Try to order ‘Data’ according to ‘exam’, in an decreasing order.

# Compute the ordering of the variable you want to sort on
order(Data$exam)
#>  [1] 10  1  9  6  4  3  7  8 12 11  2  5

# Store the ordering in an object
o <- order(Data$exam) 

#Use this object to sort the dataset according to this ordering
Data[o,] 
#>                id workshop    sex exam question1 question2 question3 total
#> 10      De Pooter        R   male  8.0         5         3         4    12
#> 1         Peeters        R female  9.0         1         1         5     7
#> 9       Verlinden     SPSS   male  9.0        NA         3         2    NA
#> 6          Steppe     SPSS female 10.0         2         3         3     8
#> 4  Van den Broeck        R female 12.0         2         2         4     8
#> 3         Pasmans     SPSS female 14.0         2         4         3     9
#> 7          Jansen        R   male 14.5         4         5         2    11
#> 8           Bosch      SAS   male 15.0         5         4         5    14
#> 12         Saenen     SPSS   male 15.0         4         4         3    11
#> 11         Maenen      SAS   male 16.0         4        NA         5    NA
#> 2        Bruyneel      SAS female 16.5         2         1         4     7
#> 5      Verbrugghe      SAS female   NA         3         1        NA    NA

Try sorting the variable ‘exam’ from highest to lowest

Data$exam[order(-Data$exam)]
#>  [1] 16.5 16.0 15.0 15.0 14.5 14.0 12.0 10.0  9.0  9.0  8.0   NA

Data$exam[order(Data$exam, decreasing = T)]
#>  [1] 16.5 16.0 15.0 15.0 14.5 14.0 12.0 10.0  9.0  9.0  8.0   NA

Try to order ‘Data’ according to ‘sex’, and ‘workshop’ in a decreasing order.

dataset_ordered <- Data[order(Data$sex,-Data$exam),]
dataset_ordered
#>                id workshop    sex exam question1 question2 question3 total
#> 2        Bruyneel      SAS female 16.5         2         1         4     7
#> 3         Pasmans     SPSS female 14.0         2         4         3     9
#> 4  Van den Broeck        R female 12.0         2         2         4     8
#> 6          Steppe     SPSS female 10.0         2         3         3     8
#> 1         Peeters        R female  9.0         1         1         5     7
#> 5      Verbrugghe      SAS female   NA         3         1        NA    NA
#> 11         Maenen      SAS   male 16.0         4        NA         5    NA
#> 8           Bosch      SAS   male 15.0         5         4         5    14
#> 12         Saenen     SPSS   male 15.0         4         4         3    11
#> 7          Jansen        R   male 14.5         4         5         2    11
#> 9       Verlinden     SPSS   male  9.0        NA         3         2    NA
#> 10      De Pooter        R   male  8.0         5         3         4    12

7. Conditional selection
Conditional selection is related to indexing, but instead of a row/column number we now put a condition that the row/column elements need to fulfill in order to be selected.

  • Try to select only the respondents that have followed the SPSS course       
  • Suppose you’re only interested in names and exam results of the people in the SPSS course. Select the appropriate columns and respondents.    
# Only respondents that have followed the SPSS course
dataset_spss1 <- Data[which(Data$workshop=="SPSS"),]
dataset_spss1
#>           id workshop    sex exam question1 question2 question3 total
#> 3    Pasmans     SPSS female   14         2         4         3     9
#> 6     Steppe     SPSS female   10         2         3         3     8
#> 9  Verlinden     SPSS   male    9        NA         3         2    NA
#> 12    Saenen     SPSS   male   15         4         4         3    11

dataset_spss2 <- subset(Data, workshop=="SPSS") 
dataset_spss2
#>           id workshop    sex exam question1 question2 question3 total
#> 3    Pasmans     SPSS female   14         2         4         3     9
#> 6     Steppe     SPSS female   10         2         3         3     8
#> 9  Verlinden     SPSS   male    9        NA         3         2    NA
#> 12    Saenen     SPSS   male   15         4         4         3    11

dataset_spss3 <- Data[Data$workshop=="SPSS",]
dataset_spss3
#>           id workshop    sex exam question1 question2 question3 total
#> 3    Pasmans     SPSS female   14         2         4         3     9
#> 6     Steppe     SPSS female   10         2         3         3     8
#> 9  Verlinden     SPSS   male    9        NA         3         2    NA
#> 12    Saenen     SPSS   male   15         4         4         3    11

# The subset function is doing almost the same as indexing, but observations with missing values are omitted.

# Keep only name and exam results
dataset_spss1_results <- dataset_spss1[,c(1,4)]
dataset_spss1_results
#>           id exam
#> 3    Pasmans   14
#> 6     Steppe   10
#> 9  Verlinden    9
#> 12    Saenen   15

dataset_spss2_results <- subset(dataset_spss2, select=c(id,exam)) 
dataset_spss2_results
#>           id exam
#> 3    Pasmans   14
#> 6     Steppe   10
#> 9  Verlinden    9
#> 12    Saenen   15

8. Splitting, stacking and merging files
- Try to create a separate data frame for males and females.
- Try to combine these two separate dataframes again using the rbind() function.
- Read in the file ‘origin.txt’ (it’s a tab-delimited format). Merge the origin dataset with ‘Data’ by the ‘id’ variable

# Separate dataset for males and females
data_males <- subset(Data, sex=="male")
data_females <- subset(Data, sex=="female")

# Combine data using rbind(..)
merged_data1 <- rbind(data_males,data_females)
merged_data1
#>                id workshop    sex exam question1 question2 question3 total
#> 7          Jansen        R   male 14.5         4         5         2    11
#> 8           Bosch      SAS   male 15.0         5         4         5    14
#> 9       Verlinden     SPSS   male  9.0        NA         3         2    NA
#> 10      De Pooter        R   male  8.0         5         3         4    12
#> 11         Maenen      SAS   male 16.0         4        NA         5    NA
#> 12         Saenen     SPSS   male 15.0         4         4         3    11
#> 1         Peeters        R female  9.0         1         1         5     7
#> 2        Bruyneel      SAS female 16.5         2         1         4     7
#> 3         Pasmans     SPSS female 14.0         2         4         3     9
#> 4  Van den Broeck        R female 12.0         2         2         4     8
#> 5      Verbrugghe      SAS female   NA         3         1        NA    NA
#> 6          Steppe     SPSS female 10.0         2         3         3     8

# Read origin data
origin <- read.delim("origin.txt", header=T)

# Merge origin and dataset by id
dataset_merged <- merge(Data,origin,by.x = "id",by.y = "id")
dataset_merged 
#>                id workshop    sex exam question1 question2 question3 total
#> 1           Bosch      SAS   male 15.0         5         4         5    14
#> 2        Bruyneel      SAS female 16.5         2         1         4     7
#> 3       De Pooter        R   male  8.0         5         3         4    12
#> 4          Jansen        R   male 14.5         4         5         2    11
#> 5          Maenen      SAS   male 16.0         4        NA         5    NA
#> 6         Pasmans     SPSS female 14.0         2         4         3     9
#> 7         Peeters        R female  9.0         1         1         5     7
#> 8          Steppe     SPSS female 10.0         2         3         3     8
#> 9  Van den Broeck        R female 12.0         2         2         4     8
#> 10     Verbrugghe      SAS female   NA         3         1        NA    NA
#> 11      Verlinden     SPSS   male  9.0        NA         3         2    NA
#>    origin
#> 1       C
#> 2       B
#> 3       B
#> 4       A
#> 5       C
#> 6       A
#> 7       B
#> 8       B
#> 9       A
#> 10      B
#> 11      A