⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03.r

📁 本程序是基于linux系统下c++代码
💻 R
字号:
#-*- R -*-## Script from Fourth Edition of `Modern Applied Statistics with S'# Chapter 3   S Languagelibrary(MASS)options(echo = T, width=65, digits=5, height=9999)# from Chapter 2powers.of.pi <- pi^(-2:2)names(powers.of.pi) <- -2:2mymat <- matrix(1:30, 3, 10)myarr <- mymatdim(myarr) <- c(3, 5, 2)dimnames(myarr) <- list(letters[1:3], NULL, c("(i)", "(ii)"))# 3.1  Language layout1 - pi + exp(1.7)a <- 6b <- a <- 6(z <- 1 - pi + exp(1.7))search()objects()objects(2)find("objects")get("[<-.data.frame", pos = 2)# hills <- hills  # only needed in S-PLUShills$ispeed <- hills$time/hills$dist# 3.2  More on S objectslength(letters)Empl <- list(employee = "Anna", spouse = "Fred", children = 3,            child.ages = c(4, 7, 9))Empl$employeeEmpl$child.ages[2]x <- "spouse"; Empl[[x]]unlist(Empl)unlist(Empl, use.names = F)attributes(myarr)attr(myarr, "dim")Empl <- c(Empl, service = 8)c(list(x = 1:3, a = 3:6), list(y = 8:23, b = c(3, 8, 39)))as(powers.of.pi, "vector")as(powers.of.pi, "numeric")is(powers.of.pi, "numeric")as(powers.of.pi, "character")is(powers.of.pi, "vector")as(powers.of.pi, "integer")is(mymat, "array")# 3.3  Arithmetical expressionsx <- c(10.4, 5.6, 3.1, 6.4, 21.7)y <- c(x, x)v <- 2 * x + y + 1s3 <- seq(-5, 5, by = 0.2)s4 <- seq(length = 51, from = -5, by = 0.2)s5 <- rep(x, times = 5) # repeat whole vectors5 <- rep(x, each = 5)  # repeat element-by-elementx <- 1:4          # puts c(1,2,3,4)             into xi <- rep(2, 4)    # puts c(2,2,2,2)             into iy <- rep(x, 2)    # puts c(1,2,3,4,1,2,3,4)     into yz <- rep(x, i)    # puts c(1,1,2,2,3,3,4,4)     into zw <- rep(x, x)    # puts c(1,2,2,3,3,3,4,4,4,4) into w( colc <- rep(1:3, each = 8) )( rowc <- rep(rep(1:4, each = 2), 3) )1 + (ceiling(1:24/8) - 1) %% 3 -> colc; colc1 + (ceiling(1:24/2) - 1) %% 4 -> rowc; rowc# orgl(3, 8)gl(4, 2, 24)# 3.4  Character vector operationspaste(c("X", "Y"), 1:4)paste(c("X", "Y"), 1:4, sep = "")paste(c("X", "Y"), 1:4, sep = "", collapse = " + ")substring(state.name[44:50], 1, 4)as.vector(abbreviate(state.name[44:50]))as.vector(abbreviate(state.name[44:50], use.classes = F))grep("na$", state.name)regexpr("na$", state.name)state.name[regexpr("na$", state.name)> 0]# 3.5  Formatting and printingd <- date()cat("Today's date is:", substring(d, 1, 10),                         substring(d, 25, 28), "\n")cat(1, 2, 3, 4, 5, 6, fill = 8, labels = letters)cat(powers.of.pi, "\n")format(powers.of.pi)cat(format(powers.of.pi), "\n", sep="  ")# 3.6  Calling conventions for functionsargs(hist.default)# 3.8  Control stucturesyp <- rpois(50, lambda = 1) # full Poisson sample of size 50table(yp)y <- yp[yp > 0]             # truncate the zeros; n = 29ybar <- mean(y); ybarlam <- ybarit <- 0                     # iteration countdel <- 1                    # iterative adjustmentwhile (abs(del) > 0.0001 && (it <- it + 1) < 10) {   del <- (lam - ybar*(1 - exp(-lam)))/(1 - ybar*exp(-lam))   lam <- lam - del   cat(it, lam, "\n")}# 3.9  Array and matrix operationsp <- dbinom(0:4, size = 4, prob = 1/3)  # an exampleCC <- -(p %o% p)diag(CC) <- p + diag(CC)structure(3^8 * CC, dimnames = list(0:4, 0:4))  # convenienceapply(iris3, c(2, 3), mean)apply(iris3, c(2, 3), mean, trim = 0.1)apply(iris3, 2, mean)ir.var <- apply(iris3, 3, var)ir.var <- array(ir.var, dim = dim(iris3)[c(2, 2, 3)],                dimnames = dimnames(iris3)[c(2, 2, 3)])matrix(rep(1/50, 50) %*% matrix(iris3, nrow = 50), nrow = 4,        dimnames = dimnames(iris3)[-1])ir.means <- colMeans(iris3)sweep(iris3, c(2, 3), ir.means)log(sweep(iris3, c(2, 3), ir.means, "/"))# 3.10  Introduction to classes and methodsmethods(summary)# End of ch03

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -