蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

在一个脚本中验证十二个主题

RWindows x64 R 4.6.2,使用基础 R。practices.R
practices.R45 行
emit <- function(key, values) {
  cat(key, "=", paste(values, collapse = ","), "\n", sep = "")
}
values <- c(4, 7, 7, 10)
emit("math", c(mean(values),median(values),sum(values),min(values),max(values),diff(values)))
emit("stats", c(var(c(1,3,5)),sd(c(1,3,5)),quantile(values,.25,names=FALSE)))
set.seed(132)
first <- rnorm(4)
set.seed(132)
emit("prob", c(identical(first,rnorm(4)), pnorm(0), round(qnorm(.975),4)))
emit("vector",c(sort(unique(values)),match(7,values),which(values>7)))
labels <- c("  ridge  "," lake ")
emit("text",c(toupper(trimws(labels)),paste(trimws(labels),collapse="/"),nchar(trimws(labels))))
frame <- data.frame(code=c("07","09","15"),visits=c(8,17,12))
detail <- data.frame(code=c("09","15"),region=c("west","north"))
joined <- merge(frame,detail,by="code")
emit("frame",c(nrow(subset(frame,visits>=12)),joined$code,joined$region))
m <- matrix(1:6,nrow=2)
emit("apply",c(apply(m,1,sum),apply(m,2,sum),unlist(lapply(list(a=1:2,b=3:5),length))))
draw <- function() {
  path <- tempfile(fileext=".png")
  on.exit(unlink(path),add=TRUE)
  png(path,width=360,height=280)
  barplot(frame$visits,names.arg=frame$code,col="#35688F",ylab="Visits")
  invisible(dev.off())
  bytes <- readBin(path,what="raw",n=8)
  identical(as.integer(bytes),c(137L,80L,78L,71L,13L,10L,26L,10L))
}
emit("plot",draw())
first_day <- as.Date("2026-04-03")
last_day <- as.Date("2026-04-06")
emit("date",c(as.numeric(difftime(last_day,first_day,units="days")),format(first_day,"%Y/%m/%d")))
groups <- factor(c("B","A","B"),levels=c("A","B","C"))
emit("list",c(length(list("x",1:4)),levels(groups),as.integer(table(groups))))
io <- function() {
  path <- tempfile(fileext=".csv")
  on.exit(unlink(path),add=TRUE)
  write.csv(frame,path,row.names=FALSE)
  reopened <- read.csv(path,colClasses=c("character","numeric"))
  c(identical(reopened$code,frame$code),sum(reopened$visits),file.exists(path))
}
emit("io",io())
bad <- c(5,NA,10)
emit("missing",c(sum(is.na(bad)),sum(bad,na.rm=TRUE),length(bad)))
本例需在 Windows x64 R 4.6.2,使用基础 R。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64 R 4.6.2,使用基础 R。

入口文件:projects/r-practice-index/practices.R

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 practices.R,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64 R 4.6.2,使用基础 R。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:R

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a317.zip

下载课程关联工程包:R

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a327.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习