Q1
讀取資料,將’sex’ 命名為 ‘Gender’
jsp <- read.csv("C:/Users/X510/Desktop/2020-03-23 Homework/2020-03-23 Homework-3/juniorSchools.txt", header = T, sep="\t")
names(jsp)[3] <- "gender"
names(jsp)## [1] "school" "class" "gender" "soc" "ravens" "pupil" "english"
## [8] "math" "year"
也可以用fix指令進行修改
Q2
檢視資料型態
## 'data.frame': 3236 obs. of 9 variables:
## $ school : Factor w/ 49 levels "S1","S10","S11",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ class : Factor w/ 4 levels "C1","C2","C3",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ gender : Factor w/ 2 levels "B","G": 2 2 2 1 1 1 1 1 1 1 ...
## $ soc : int 9 9 9 2 2 2 2 2 9 9 ...
## $ ravens : int 23 23 23 15 15 22 22 22 14 14 ...
## $ pupil : Factor w/ 1192 levels "P1","P10","P100",..: 1 1 1 413 413 512 512 512 612 612 ...
## $ english: int 72 80 39 7 17 88 89 83 12 25 ...
## $ math : int 23 24 23 14 11 36 32 39 24 26 ...
## $ year : int 0 1 2 0 1 0 1 2 0 1 ...
將變項soc轉換為factor
## 'data.frame': 3236 obs. of 9 variables:
## $ school : Factor w/ 49 levels "S1","S10","S11",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ class : Factor w/ 4 levels "C1","C2","C3",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ gender : Factor w/ 2 levels "B","G": 2 2 2 1 1 1 1 1 1 1 ...
## $ soc : Factor w/ 9 levels "1","2","3","4",..: 9 9 9 2 2 2 2 2 9 9 ...
## $ ravens : int 23 23 23 15 15 22 22 22 14 14 ...
## $ pupil : Factor w/ 1192 levels "P1","P10","P100",..: 1 1 1 413 413 512 512 512 612 612 ...
## $ english: int 72 80 39 7 17 88 89 83 12 25 ...
## $ math : int 23 24 23 14 11 36 32 39 24 26 ...
## $ year : int 0 1 2 0 1 0 1 2 0 1 ...
依題目重新命名soc類別
levels(jsp$soc) <- c("I", "II", "III_0man", "III_man", "IV", "V", "VI_Uemp_L", "VII_emp_NC", "VIII_Miss_Dad")
head(jsp$soc)## [1] VIII_Miss_Dad VIII_Miss_Dad VIII_Miss_Dad II II
## [6] II
## Levels: I II III_0man III_man IV V VI_Uemp_L VII_emp_NC VIII_Miss_Dad
Q3
Save the edited jsp data
Read the edited jsp data
## school class gender soc ravens pupil english math year
## 1 S1 C1 G VIII_Miss_Dad 23 P1 72 23 0
## 2 S1 C1 G VIII_Miss_Dad 23 P1 80 24 1
## 3 S1 C1 G VIII_Miss_Dad 23 P1 39 23 2
## 4 S1 C1 B II 15 P2 7 14 0
## 5 S1 C1 B II 15 P2 17 11 1
## 6 S1 C1 B II 22 P3 88 36 0