データの読み込みと平均出席率の算出

データは1回から6回までの出欠です。1が出席で0が欠席です。

# import a library
library(data.table)
library(ggplot2)
# load the data
df <- read.csv("https://pastebin.com/raw/SfF7QiwE")
dt <- data.table(df)
head(dt)
##    id l1 l2 l3 l4 l5 l6 class
## 1:  1  0  1  1  0  0  0    w1
## 2:  2  0  1  0  0  0  0    w1
## 3:  3  1  1  1  1  1  1    w1
## 4:  4  1  1  1  1  1  1    w1
## 5:  5  1  1  1  1  1  1    w1
## 6:  6  1  1  1  1  1  1    w1

data.tableに含まれるmelt()でよこ長データをたて長に変換します。

# convert wide to long
dt_long <- melt(data = dt,
     id.vars = c("id", "class"),
     measure.vars = c("l1","l2","l3","l4","l5","l6"),
     variable.name = "lecture",
     value.name = "attendance")
head(dt_long, 10)
##     id class lecture attendance
##  1:  1    w1      l1          0
##  2:  2    w1      l1          0
##  3:  3    w1      l1          1
##  4:  4    w1      l1          1
##  5:  5    w1      l1          1
##  6:  6    w1      l1          1
##  7:  7    w1      l1          1
##  8:  8    w1      l1          1
##  9:  9    w1      l1          1
## 10: 10    w1      l1          1

授業の回数ごとの出席率を算出します。

# percentage of attendance for each lecture
dt_lecture <- dt_long[, .(attendance = mean(attendance)*100), by = lecture]
dt_lecture
##    lecture attendance
## 1:      l1   96.93878
## 2:      l2   93.87755
## 3:      l3   87.75510
## 4:      l4   84.69388
## 5:      l5   94.89796
## 6:      l6   90.81633

ライン・チャートを作成します。

# create a line graph
ggplot(data=dt_lecture, aes(x=lecture, y=attendance, group=1)) +
  geom_line() +
  geom_point() +
  labs(title="Attendance for each lecture")

クラスごとの出席率を集計します。

# percentage of attendance by class
dt_class <- dt_long[, .(attendance = mean(attendance)*100), by = class]
dt_class
##    class attendance
## 1:    w1   90.57971
## 2:    f4   87.87879
## 3:    w2   94.44444
## 4:   th4   92.30769

バー・チャートを作成します。

# create a bar graph
ggplot(data=dt_class, aes(x=class, y=attendance)) + 
  geom_bar(stat="identity") + 
  ggtitle("Attendance by class")

f4クラスの学生の出席率を取得

# attendance in f4
dt_f4 <- dt_long[class=="f4",.(mean(attendance)*100),id]
dt_f4
##     id        V1
##  1: 24  83.33333
##  2: 25 100.00000
##  3: 26  50.00000
##  4: 27  33.33333
##  5: 28  66.66667
##  6: 29 100.00000
##  7: 30 100.00000
##  8: 31 100.00000
##  9: 32 100.00000
## 10: 33 100.00000
## 11: 34  83.33333
## 12: 35  66.66667
## 13: 36 100.00000
## 14: 37 100.00000
## 15: 38 100.00000
## 16: 39 100.00000
## 17: 40  83.33333
## 18: 41 100.00000
## 19: 42 100.00000
## 20: 43 100.00000
## 21: 44 100.00000
## 22: 45  66.66667
##     id        V1

Tukey’s Method (Box Whisker)を作成します。

2件のはずれ値が検出されました。

ggplot(dt_f4, aes(x = "", y=V1)) +
  geom_boxplot(outlier.colour="red",
               outlier.shape=16,
               outlier.size=4, notch=FALSE)