#先把資料叫出來
library(HSAUR3)
## Loading required package: tools
dta1 <- backpain
head(dta1) #看一下資料
## ID status driver suburban
## 1 1 case yes yes
## 2 1 control yes no
## 3 2 case yes yes
## 4 2 control yes yes
## 5 3 case yes no
## 6 3 control yes yes
library(magrittr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#使用group_by,先將資料分層後再做計算
dta1 %>% group_by (driver, suburban) %>%
summarise(case=sum(status =='case'),
control=sum(status=='control'),
total=n())
## # A tibble: 4 x 5
## # Groups: driver [2]
## driver suburban case control total
## <fct> <fct> <int> <int> <int>
## 1 no no 26 47 73
## 2 no yes 6 7 13
## 3 yes no 64 63 127
## 4 yes yes 121 100 221
Is there anything interesting to report?
#先把資料叫出來
library(datasets)
#看一下第一筆資料的型態
class(state.x77)
## [1] "matrix"
#資料型態更改成 data.frame,並指定資料名稱
dta2_1 <- as.data.frame(state.x77)
#確認第二筆資料的型態
class(USArrests)
## [1] "data.frame"
#指定資料名稱
dta2_2 <-USArrests
#合併兩筆資料,並移除遺漏值
dta2 <- merge(dta2_1, dta2_2, rm.na =T)
#計算相關係數,並畫圖找出關聯性
library(corrplot)
## corrplot 0.84 loaded
round(cor(dta2),2) %>% corrplot(., method= "number")
##Murder與 Illiteracy, Assault, Rape有強烈的正相關; ##Illiteracy與Life Exp, HS Grd, Fros有強烈的負相關。
```