I. My class on Moodle

Let me show the seventh lecture as a sample. Students are supposed to submit “ノート提出” in every lecture. Each “ノート提出” is graded 10. So the marks can be used to calculate the percentage of attendance.

II. Download the data

  1. Navigation>My courses>YOUR LECTURE>Grades

  2. Choose “Plain text file” in the box under your User picture.

  3. Click on “Download” on the bottom of the page.

III. Load the data

# import libraries
library(tidyverse)
library(janitor) # 列名をきれいにする
# load the data
df0 <- read_csv("data/comEmg2_2.csv")

You can look at df0 in RStudio.

IV. Convert data from wide to long

You can manipulate long formatted data more efficiently than wide ones. Execute the following code to clean the data and reshape it from wide to long format.

# import libraries
library(tidyverse)
library(janitor) # 列名をきれいにする
# load the data
df0 <- read_csv("data/comEmg2_2.csv")
df0 %>%
  clean_names() %>% # 列名をきれいにして、
  select(id_number, 11:24) %>% # 列を選択して、
  slice(-8) %>% # 不要な行を削除して、
  mutate_all(funs(str_replace(., "-", "NA"))) %>% # -をNAにかえて、 
  type_convert() %>% # 文字を数値にかえて、
  pivot_longer(-id_number, # よこ長データをたて長にして、
               names_to = "lecture", # 新たな列名をlectureして
               names_prefix = "assignment_noto_ti_chu", # 列名のから除いて、
               values_to = "score") -> df1 # 値列の列名をscoreに
df1
## # A tibble: 980 x 3
##    id_number lecture        score
##    <chr>     <chr>          <dbl>
##  1 g3220069  1_10dian_real     10
##  2 g3220069  2_10dian_real     10
##  3 g3220069  3_10dian_real     10
##  4 g3220069  4_10dian_real     10
##  5 g3220069  5_10dian_real     10
##  6 g3220069  6_10dian_real     10
##  7 g3220069  7_10dian_real     10
##  8 g3220069  8_10dian_real     10
##  9 g3220069  9_10dian_real     10
## 10 g3220069  10_10dian_real    10
## # … with 970 more rows

V. Calculate the percentage of attendance

10 stands for attendance and 0 stands for absence. You can easily get the percentage of attendance of each lecture with the following code.

df1 %>% # df1の
  group_by(lecture) %>% # lecture列でグループ分けし、
  replace_na(list(score = 0)) %>% # NAを0にかえて、
  summarize(mean = round(mean(score),2)*10) -> df2 # 各回の出席率を集計
df2
## # A tibble: 14 x 2
##    lecture         mean
##    <chr>          <dbl>
##  1 1_10dian_real   82.9
##  2 10_10dian_real  71.4
##  3 11_10dian_real  74.3
##  4 12_10dian_real  74.3
##  5 13_10dian_real  74.3
##  6 14_10dian_real  74.3
##  7 2_10dian_real   82.9
##  8 3_10dian_real   78.6
##  9 4_10dian_real   75.7
## 10 5_10dian_real   81.4
## 11 6_10dian_real   80  
## 12 7_10dian_real   78.6
## 13 8_10dian_real   75.7
## 14 9_10dian_real   74.3

VI. Create a bar char

# create a bar chart with values inside bars
ggplot(df2, aes(x=lecture, y=mean)) +
  geom_bar(stat="identity", fill="steelblue")+
  geom_text(aes(label=mean), vjust=1.6, color="white", size=3.5)+
  theme_minimal() +
  labs(
  x = "提出回",
  y = "提出割合",
  title = "コンピュータ英語II(水2)のノート2−14の提出率") +
  theme_gray (base_family = "HiraKakuPro-W3") # 日本語を表示