emit <- function(key, values) {
cat(key, "=", paste(values, collapse = ","), "\n", sep = "")
}
emit("decimal", c(64, -0.625, 7.5e2, -7E-2))
value <- FALSE
kind_before <- typeof(value)
value <- "shore"
emit("types", c(typeof(7), typeof(7L), kind_before, typeof(value),
'shore' == "shore", exists("true", inherits = FALSE)))
count <- c(14, 8, 20, 11, 17)
emit("indices", c(count[2], count[1:3], count[c(1, 3, 5)], count[-c(1, 5)]))
emit("vector_math", c(c(3, 8) + c(2, 7), count - 3, c(3, 5) ^ 2, sqrt(c(9, 16))))
warned <- FALSE
uneven <- withCallingHandlers(c(3, 6, 9) + c(1, 2), warning = function(w) {
warned <<- TRUE
invokeRestart("muffleWarning")
})
emit("recycling", c(c(3, 6, 9, 12) + c(1, 2), uneven, warned))
order_by_count <- order(count)
emit("ordering", c(sort(count), rev(count), order_by_count, count[order_by_count]))
numbers <- c(2, 4, 6, 8, 10)
emit("statistics", sprintf("%.6f", c(sum(numbers), mean(numbers), var(numbers), sd(numbers),
min(numbers), max(numbers), range(numbers))))
emit("generation", c(seq(2, 8, by = 2), seq(0, 2, length.out = 3), rep(7, 3)))
missing_values <- c(NA_integer_, 4L, NULL, NA_integer_)
emit("missing", c(length(missing_values), is.na(missing_values), length(NULL), is.null(NULL)))
marks <- c(59, 67, NA, 70, 62, 60)
keep <- marks >= 60 & marks < 70
emit("mask", keep)
emit("selection", c(which(keep), marks[which(keep)]))
emit("aggregate_logic", c(all(c(TRUE, TRUE)), all(c(TRUE, FALSE)),
any(c(FALSE, TRUE)), any(c(FALSE, FALSE)),
all(c(TRUE, NA)), any(c(FALSE, NA))))
word <- intToUtf8(c(0x6e56, 0x6cca))
encoded <- iconv(word, from = "UTF-8", to = "CP936", toRaw = TRUE)[[1L]]
emit("encoding", c(nchar(word, type = "chars"), nchar(word, type = "bytes"),
length(charToRaw(word)), length(encoded)))
emit("strings", c(toupper("Tidal"), tolower("Tidal"), substr("north-east", 1, 5),
substring("north-east", 7), as.numeric("27.5"), as.character(7.25),
strsplit("west:lake:3", ":", fixed = TRUE)[[1]],
gsub("/", "-", "2026/11/9", fixed = TRUE),
gsub("[[:alpha:]]+", "#", "Bay Road"),
gsub("\\d+", "?", "Station 87", perl = TRUE)))
raw <- c(2, 4, 6, 8, 10, 12)
column_fill <- matrix(raw, nrow = 2, ncol = 3)
row_fill <- matrix(raw, nrow = 2, ncol = 3, byrow = TRUE,
dimnames = list(c("west", "east"), c("a", "b", "c")))
emit("matrix_fill", c(column_fill[1, ], row_fill[1, ], row_fill[1, 3], row_fill["east", ]))
emit("matrix_ops", c(row_fill[1, ] * 2, (row_fill + row_fill)[2, ],
(row_fill - row_fill)[1, ], (row_fill / row_fill)[1, ],
(row_fill * row_fill)[1, ], c(row_fill[1, , drop = FALSE] %*% c(1, 0, 2))))
emit("apply", c(apply(row_fill, 1, sum), apply(row_fill, 2, sum)))
system <- matrix(c(2, 1, 1, 3), 2)
emit("solve", c(round(c(solve(system)), 3), solve(system, c(4, 7))))
emit("shapes", c(typeof(list(1, FALSE)), paste(dim(array(1:8, c(2,2,2))), collapse = "x"),
paste(levels(factor(c("wet", "dry"))), collapse = "/"),
paste(dim(data.frame(site = c("west", "east"), count = c(4,7))), collapse = "x")))
本例需在 基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。
入口文件:projects/r-types-and-decisions/types.R
本地运行步骤快速上手
- 点击上方“保存源码”保存为
types.R,或下载右侧课程工程包。 - 确认本地已准备 基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a317.zip
课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202612a328.zip