Dear all WISE R Club members. This is the solution to our first assignment. I hope you guys be more familiar with R after reading the book and finishing assignment 1.
First of all, we load
grades <- read.table("grades.txt", header=TRUE,stringsAsFactors=FALSE)
# `stringsAsFactors=FALSE` prevent the `read.table` function from treating the character name as factor
# `grades` is a data frame
After creating the dataframe you can see what’s in it using the following functions.
head(grades)
## name Math Chinese English
## 1 丁聪华 63 80 77
## 2 孙蝶妃 73 81 74
## 3 许娇翔 78 88 82
## 4 崔子希 74 76 86
## 5 樊瑶芳 81 84 86
## 6 阮恭琴 88 83 92
str(grades)
## 'data.frame': 300 obs. of 4 variables:
## $ name : chr "丁聪华" "孙蝶妃" "许娇翔" "崔子希" ...
## $ Math : int 63 73 78 74 81 88 84 96 78 96 ...
## $ Chinese: int 80 81 88 76 84 83 85 89 82 76 ...
## $ English: int 77 74 82 86 86 92 92 98 81 91 ...
We plot the histogram of the grade of math.
hist(grades$Math,col="lightblue",main="The histogram of the grade of math",xlab="The grade of math")
Now we plot the scatterplot of Math vs. Chinese
and Math vs. English
in one long graph.
opar <- par(no.readonly=TRUE)
par(mfcol=c(2,1))
par(lwd=1.5)
attach(grades)
plot(Math,Chinese,col="red",main="Scatterplot of Math vs. Chinese")
abline(lm(Math~Chinese))
plot(Math,English,col="blue",main="Scatterplot of Math vs. English")
abline(lm(Math~English))
detach(grades)
par(opar)
To see how many students have passed this exam.
pass <- subset(grades,Math>=60 & Chinese>=60 & English>=60, select=name)
dim(pass)[1]
## [1] 296
So in total 296 students have passed the exam.
To find the out the standing student.
outstand <- subset(grades,Math>=90 & Chinese>=90 & English>=90, select=name)
outstand
## name
## 242 郭文权
So the headmaster should award 郭文权. Congratulations!
The solution is not unique.
Someone told me that Chinese characters cann’t display normally in R. I have to say R is friendly enough to Chinese character. If there is something wrong when you try to display Chinese character, it’s highly probably that you made a mistakes when loading the data, that is, you didn’t specify stringsAsFactors=FALSE
.
To display that R is friendly enough to Chinese character I can give you an example.
变量1 <- 2
变量2 <- 3
变量1+变量2
## [1] 5
变量1*变量2
## [1] 6
As you see, the Chinese characters can even be the variable name. How amazing is it!
If you have more questions about this solution, you can send an email to me or ask me in the next meeting.
Thank you!
The end.
WISE R Club project is maintained by XiaojunSun.