Report

Report lab1 introduction

This is our first report from descriptive statistics. We hope You enjoy it :)!

Gather instruction

# 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<-
  gather(
  student_data,
  key = "exam_type",
  value = "points",
  -student_id, -name,
  
  na.rm = FALSE,
  convert = FALSE,
  factor_key = FALSE
)

# Print first few rows:
head(student_data_long)
##   student_id     name exam_type points
## 1          1 Student1  midterm1     65
## 2          2 Student2  midterm1     83
## 3          3 Student3  midterm1     78
## 4          4 Student4  midterm1     62
## 5          5 Student5  midterm1     71
## 6          6 Student6  midterm1     89

Separate instruction

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
separate(
  separate(
  student_data2,
  name_age,
  into=c("name", "age"),
  sep = "_",
  remove = TRUE,
  convert = FALSE,
  extra = "warn",
  fill = "warn",
  
),
# Split variables exam_scores to separate columns for each exam type
  exam_scores,
  into=c("midterm1_score", "midterm2_score", "final_score"),
  sep = ",",
  remove = TRUE,
  convert = FALSE,
  extra = "warn",
  fill = "warn",
) -> student_separated

head(student_separated)
##   student_id    name age midterm1_score midterm2_score final_score
## 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

Complete instruction

# Complete missing combinations for students
complete(student_data2, name_age, exam_scores, fill = list(), explicit = TRUE)->student_complete
student_complete%>%relocate(student_id)->student_complete
print(student_complete[10:20, ])
## # A tibble: 11 × 3
##    student_id name_age exam_scores                     
##         <int> <chr>    <chr>                           
##  1         47 Alice_20 midterm1_75,midterm2_78,final_80
##  2         NA Alice_20 midterm1_80,midterm2_85,final_75
##  3         NA Alice_20 midterm1_82,midterm2_80,final_85
##  4         NA Alice_20 midterm1_85,midterm2_86,final_88
##  5         NA Alice_20 midterm1_88,midterm2_90,final_92
##  6         NA Bob_22   midterm1_75,midterm2_78,final_80
##  7         NA Bob_22   midterm1_80,midterm2_85,final_75
##  8          3 Bob_22   midterm1_82,midterm2_80,final_85
##  9          8 Bob_22   midterm1_82,midterm2_80,final_85
## 10         13 Bob_22   midterm1_82,midterm2_80,final_85
## 11         18 Bob_22   midterm1_82,midterm2_80,final_85

Spread instruction

# Transform data to wide format

separate(student_data2, name_age, into=c("name", "age"), sep="_")->student_spread
separate(student_spread, exam_scores, into=c("midterm1", "midterm2", "final"), sep=",")->student_spread
gather(
  student_spread, 
  "exam", 
  "scores",
  -student_id, -name, -age,
  )->student_spread
separate(student_spread, scores, into=c("midterm", "score"), sep="_")->student_spread
student_spread[, -5]->student_spread
spread(student_spread, exam, score)->student_spread
head(student_spread)
##   student_id    name age final midterm1 midterm2
## 1          1    John  21    75       80       85
## 2          2   Alice  20    80       75       78
## 3          3     Bob  22    85       82       80
## 4          4   Emily  23    92       88       90
## 5          5 Michael  22    88       85       86
## 6          6    John  21    75       80       85

Unite instruction

student_data_unite <- unite(student_separated, name_age, name:age, sep = "_", remove = TRUE)
head(student_data_unite)
##   student_id   name_age midterm1_score midterm2_score final_score
## 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