Exercise 1

#檢視資料
pacman::p_load(Ecdat, dplyr, tidyr)
data(Caschool, package="Ecdat")
Caschool |> head() 
  distcod  county                        district grspan enrltot teachers
1   75119 Alameda              Sunol Glen Unified  KK-08     195    10.90
2   61499   Butte            Manzanita Elementary  KK-08     240    11.15
3   61549   Butte     Thermalito Union Elementary  KK-08    1550    82.90
4   61457   Butte Golden Feather Union Elementary  KK-08     243    14.00
5   61523   Butte        Palermo Union Elementary  KK-08    1335    71.50
6   62042  Fresno         Burrel Union Elementary  KK-08     137     6.40
  calwpct mealpct computer testscr   compstu  expnstu      str    avginc
1  0.5102  2.0408       67  690.80 0.3435898 6384.911 17.88991 22.690001
2 15.4167 47.9167      101  661.20 0.4208333 5099.381 21.52466  9.824000
3 55.0323 76.3226      169  643.60 0.1090323 5501.955 18.69723  8.978000
4 36.4754 77.0492       85  647.70 0.3497942 7101.831 17.35714  8.978000
5 33.1086 78.4270      171  640.85 0.1280899 5235.988 18.67133  9.080333
6 12.3188 86.9565       25  605.55 0.1824818 5580.147 21.40625 10.415000
      elpct readscr mathscr
1  0.000000   691.6   690.0
2  4.583333   660.5   661.9
3 30.000002   636.3   650.9
4  0.000000   651.9   643.5
5 13.857677   641.8   639.9
6 12.408759   605.7   605.4
#參考同學作業隨機抽取學校
set.seed(1234)
t1<-Caschool
random <- group_by(t1, county) %>% 
  sample_n(1)

pacman::p_load("basicPlotteR")

with(random,  
  plot(mathscr, readscr,
                      xlab = "Average math score", 
                      ylab = "Average reading score",
                      pch = 20, bty="n", col="lightblue",
                      axes = FALSE))

axis(1, at = seq(600,750, by=10))
axis(2, at = seq(600,750, by=10))
grid()

Exercis 2

#檢查數據
pacman::p_load(MASS)
data(nlschools, package="MASS")
t2 <- nlschools
head(t2)
  lang   IQ class GS SES COMB
1   46 15.0   180 29  23    0
2   45 14.5   180 29  10    0
3   33  9.5   180 29  15    0
4   46 11.0   180 29  23    0
5   20  8.0   180 29  10    0
6   30  9.5   180 29  10    0
#求置信區間
t2 %>% 
  group_by(class) %>% 
  summarize(language_mean <- mean(lang),
            language_lb <- language_mean - 1.96*sd(lang),
            language_ub <- language_mean + 1.96*sd(lang)) %>% arrange(class)
# A tibble: 133 x 4
   class `language_mean <- mean(lang)` `language_lb <- lan~ `language_ub <- lan~
   <fct>                         <dbl>                <dbl>                <dbl>
 1 180                            36.4                11.3                  45.6
 2 280                            23.7                16.0                  40.8
 3 1082                           30.4                 8.51                 48.3
 4 1280                           30.9                14.8                  42.1
 5 1580                           30.9                 2.35                 54.5
 6 1680                           41.5                16.1                  40.8
 7 1880                           32.9                 7.91                 48.9
 8 2180                           38.8                 9.36                 47.5
 9 2480                           45.0                13.1                  43.8
10 2680                           40.7                13.5                  43.3
# ... with 123 more rows

Exercise 3

#檢視資料
pacman::p_load(carData)
data(OBrienKaiser, package = "carData")
t3<-OBrienKaiser
head(t3)
  treatment gender pre.1 pre.2 pre.3 pre.4 pre.5 post.1 post.2 post.3 post.4
1   control      M     1     2     4     2     1      3      2      5      3
2   control      M     4     4     5     3     4      2      2      3      5
3   control      M     5     6     5     7     7      4      5      7      5
4   control      F     5     4     7     5     4      2      2      3      5
5   control      F     3     4     6     4     3      6      7      8      6
6         A      M     7     8     7     9     9      9      9     10      8
  post.5 fup.1 fup.2 fup.3 fup.4 fup.5
1      2     2     3     2     4     4
2      3     4     5     6     4     1
3      4     7     6     9     7     6
4      3     4     4     5     3     4
5      3     4     3     6     4     3
6      9     9    10    11     9     6
#寬轉長試圖用spread函數但失敗
#t3 %>%spread(key = row.names,value =?)

#嘗試矩陣轉置但失敗 
t4 <- matrix(t3[,3:17])  
t5 <- t(t4)

Exercise 4

#讀取數據
majors <- read.table("C:\\Users\\shabby\\Desktop\\DataManagement\\week6\\student_major.txt", header = TRUE)

sports <-read.table("C:\\Users\\shabby\\Desktop\\DataManagement\\week6\\student_sport.txt", header = TRUE)

cafeteria <-read.table("C:\\Users\\shabby\\Desktop\\DataManagement\\week6\\student_cafe.txt", header = TRUE)

#合併表格,根據ID
t6 <- merge(majors, sports, by="id", all = TRUE)
t7 <- merge(cafeteria,t6,by="id", all = TRUE)