毒菇求败gocrazy 发表于 2017-8-16 11:09 
第一个pmin是返回小于100的元素,pmax是返回大于0的元素的意思吧,但是dat_simscore还没声明怎么能用它去 ...
晕,要点“复制代码”,用鼠标选会出错的
## generate sim data
set.seed(123)
dat_sim <- data.frame(id = 1:1000,
score = rnorm(1000, 60, 20))
dat_sim$score <- pmin(dat_sim$score, 100)
dat_sim$score <- pmax(0, dat_sim$score)
## split and plot
red <- dat_sim$score[dat_sim$score < 60]
blue <- dat_sim$score[dat_sim$score >= 60 & dat_sim$score < 80]
green <- dat_sim$score[dat_sim$score >= 80]
draw_hist <- function(draw_data, col_name) {
hist(draw_data, col = col_name, xlim = c(20,100), ylim = c(0,200),
breaks = c(10*2:10),
xlab = "Name", ylab = "Score", main = "Score")
}
draw_hist(red, "red")
par(new=T)
draw_hist(blue, "blue")
par(new=T)
draw_hist(green, "green")