Data Wrangling Report

EXERCISE 1

set.seed(42)
# Data
student_data <- data.frame(
  student_id = 1:50,
  name = paste0("Student", 1:50),
  midterm1 = sample(60:100, 50, replace = TRUE),
  midterm2 = sample(60:100, 50, replace = TRUE),
  final = sample(60:100, 50, replace = TRUE)
)

# Transform data to long format using gather:
student_data_long <- student_data %>% gather(exam_type, score, midterm1:final)
# Print first few rows:
head(student_data_long)
##   student_id     name exam_type score
## 1          1 Student1  midterm1    96
## 2          2 Student2  midterm1    60
## 3          3 Student3  midterm1    84
## 4          4 Student4  midterm1    69
## 5          5 Student5  midterm1    95
## 6          6 Student6  midterm1    77

EXERCISE 2

set.seed(42)
# Data
student_data2 <- data.frame(
  student_id = 1:50,
  name_age = c("John_21", "Alice_20", "Bob_22", "Emily_23", "Michael_22"),
  exam_scores = c("midterm1_80,midterm2_85,final_75", "midterm1_75,midterm2_78,final_80", "midterm1_82,midterm2_80,final_85", "midterm1_88,midterm2_90,final_92", "midterm1_85,midterm2_86,final_88")
)

# Split variables name_age to 2 separate columns: name and age
student_data2_split <- student_data2 %>% 
  separate(name_age, c("name", "age")) %>%

# Split variables exam_scores to separate columns for each exam type
  separate(exam_scores, c("midterm1", "midterm2", "final"), sep = ",") %>%
  separate(midterm1, c("exam_name", "midterm1"), sep = "_") %>% 
  select(-exam_name) %>%
  separate(midterm2, c("exam_name", "midterm2"), sep = "_") %>% 
  select(-exam_name) %>%
  separate(final, c("exam_name", "final"), sep = "_") %>% 
  select(-exam_name)
# Transform student_data2 to long format
student_data2_long <- student_data2_split %>%
  pivot_longer(cols = c(midterm1, midterm2, final), names_to = "exam_name", values_to = "exam_score")
head(student_data2_split)
##   student_id    name age midterm1 midterm2 final
## 1          1    John  21       80       85    75
## 2          2   Alice  20       75       78    80
## 3          3     Bob  22       82       80    85
## 4          4   Emily  23       88       90    92
## 5          5 Michael  22       85       86    88
## 6          6    John  21       80       85    75
head(student_data2_long)
## # A tibble: 6 × 5
##   student_id name  age   exam_name exam_score
##        <int> <chr> <chr> <chr>     <chr>     
## 1          1 John  21    midterm1  80        
## 2          1 John  21    midterm2  85        
## 3          1 John  21    final     75        
## 4          2 Alice 20    midterm1  75        
## 5          2 Alice 20    midterm2  78        
## 6          2 Alice 20    final     80

EXERCISE 3

set.seed(42)
# Data
student_data3 <- data.frame(
  student_id = 1:50,
  name_age = c("John_21", "Alice_20", "Bob_22", "Emily_23", "Michael_22"),
  exam_scores = c("midterm1_80,midterm2_85,final_75", "midterm1_75,midterm2_78,final_80", "midterm1_82,midterm2_80,final_85", "midterm1_88,midterm2_90,final_92", "midterm1_85,midterm2_86,final_88")
)

# Complete missing combinations for students
student_data_complete <- student_data3 %>%
  complete(student_id = full_seq(student_id, period = 1), 
           fill = list(name_age = "unknown", exam_scores = 0))
head(student_data_complete)
## # A tibble: 6 × 3
##   student_id name_age   exam_scores                     
##        <dbl> <chr>      <chr>                           
## 1          1 John_21    midterm1_80,midterm2_85,final_75
## 2          2 Alice_20   midterm1_75,midterm2_78,final_80
## 3          3 Bob_22     midterm1_82,midterm2_80,final_85
## 4          4 Emily_23   midterm1_88,midterm2_90,final_92
## 5          5 Michael_22 midterm1_85,midterm2_86,final_88
## 6          6 John_21    midterm1_80,midterm2_85,final_75

EXERCISE 4

set.seed(42)
# Data
student_data4 <- data.frame(
  student_id = 1:50,
  name_age = c("John_21", "Alice_20", "Bob_22", "Emily_23", "Michael_22"),
  exam_scores = c("midterm1_80,midterm2_85,final_75", "midterm1_75,midterm2_78,final_80", "midterm1_82,midterm2_80,final_85", "midterm1_88,midterm2_90,final_92", "midterm1_85,midterm2_86,final_88")
)


# Transform data to wide format
student_data_wide <- student_data4 %>%
  unite(student, student_id, name_age, sep = ".") %>%
  pivot_wider(names_from = "student", values_from = "exam_scores")
head(student_data_wide)
## # A tibble: 1 × 50
##   `1.John_21`    `2.Alice_20` `3.Bob_22` `4.Emily_23` `5.Michael_22` `6.John_21`
##   <chr>          <chr>        <chr>      <chr>        <chr>          <chr>      
## 1 midterm1_80,m… midterm1_75… midterm1_… midterm1_88… midterm1_85,m… midterm1_8…
## # ℹ 44 more variables: `7.Alice_20` <chr>, `8.Bob_22` <chr>,
## #   `9.Emily_23` <chr>, `10.Michael_22` <chr>, `11.John_21` <chr>,
## #   `12.Alice_20` <chr>, `13.Bob_22` <chr>, `14.Emily_23` <chr>,
## #   `15.Michael_22` <chr>, `16.John_21` <chr>, `17.Alice_20` <chr>,
## #   `18.Bob_22` <chr>, `19.Emily_23` <chr>, `20.Michael_22` <chr>,
## #   `21.John_21` <chr>, `22.Alice_20` <chr>, `23.Bob_22` <chr>,
## #   `24.Emily_23` <chr>, `25.Michael_22` <chr>, `26.John_21` <chr>, …

EXERCISE 5

set.seed(42)
# Data
student_data5 <- data.frame(
  student_id = 1:50,
  name_age = c("John_21", "Alice_20", "Bob_22", "Emily_23", "Michael_22"),
  exam_scores = c("midterm1_80,midterm2_85,final_75", "midterm1_75,midterm2_78,final_80", "midterm1_82,midterm2_80,final_85", "midterm1_88,midterm2_90,final_92", "midterm1_85,midterm2_86,final_88")
)

student_data5_unite <- student_data2 %>% 
  separate(name_age, c("name", "age")) %>%
# Merge variables name and age to one column called name_age
  unite(name_age, name, age, sep = "_")
head(student_data5)
##   student_id   name_age                      exam_scores
## 1          1    John_21 midterm1_80,midterm2_85,final_75
## 2          2   Alice_20 midterm1_75,midterm2_78,final_80
## 3          3     Bob_22 midterm1_82,midterm2_80,final_85
## 4          4   Emily_23 midterm1_88,midterm2_90,final_92
## 5          5 Michael_22 midterm1_85,midterm2_86,final_88
## 6          6    John_21 midterm1_80,midterm2_85,final_75