######################################## ############# All programs in this script come from: appendix ############################### ###################### Practice 1 (first few steps) X = (x =) sample (x, 20) set. seed (0); sample (, 3) z = sample (, 100000) Z [] Y = C) Z [y] (Z = sample (x, 100, rep = t) (z1 = unique (z) length (Z1) xz = setdiff (x, z) sort (Union (xz Z, z) setequal (Union (xz Z, Z), x) intersect (,) sample (, 20, prob =) # practice 2 (some simple operations) pI * 10 ^ 2 "*" (PI, "^" () pI) ^-2.3x = pI * 10 ^ 2 xprint (x) (X = pI * 10 ^ 2) PI ^ () print (x, digits = 12) # Practice 3 (about R object types, etc.) x = pI * 10 ^ 2 class (x) typeof (x) Class (CARs) typeof (CARs) Names (CARs) summary (CARs) Head (CARs) tail (CARs) STR (CARs) Row. names (CARs) attributes (CARs) Class (Dist ~ Speed) plot (Dist ~ Speed, cars) plot (CARS $ speed, cars $ Dist) # practice 4 (including regression of quantitative and qualitative variables as simple independent variables) ncol (CARs) nrow (CARs) dim (CARs) lm (Dist ~ Speed, Data = cars) cars $ qspeed = cut (CARS $ speed, breaks = quantile (CARS $ speed), include. lowest = true) Names (CARs) cars [3] Table (cars [3]) is. factor (CARS $ qspeed) plot (Dist ~ Qspeed, Data = cars) (a = LM (Dist ~ Qspeed, Data = cars) Summary (a) # practice 5 (simple sample description statistics) x <-round (runif (20, 0, 20), digits = 2) Summary (X) min (x) max (x) range (x) median (x) mean (x) var (x) SD (x) SQRT (VAR (x) rank (X) order (x) Order (x, decreasing = t) x [Order (x)] Sort (x) sort (x, decreasing = T) sort (x, dec = T) sum (x) length (x) round (x, 0) round (x, 5) fivenum (x) quantile (X, C (0 ,. 33 ,. 66,1) mad (x) cummax (x) cummin (x) cumprod (x) Cor (x, sin (X/20) # Practice 6 (simple graphics) X = rnorm (200) hist (X, Col = "light blue") rug (x) stem (x) x <-rnorm (500) Y <-x + rnorm (500) plot (Y ~ X) A = LM (Y ~ X) abline (A, Col = "red") print ("Hello world! ") Paste (" minimum value of X = ", min (x) demo (graphics) # Practice 7 (plural operation and function extreme value) (2 + 4I) ^-3.5 + (2I + 4.5) * (-1.7-2.3i)/(2.6-7i) * (-4 + 5.1i )) (z <-complex (real = rnorm (10), imaginary = rnorm (10) complex (RE = rnorm (3), Im = rnorm (3 )) re (z) IM (z) mod (z) Arg (z) choose (3, 2) factorial (6) F = function (x) x ^ 3-2 * x-1uniroot (F, C () F = function (x) x ^ 2 + 2 * x + 1 optimize (F, C (-) # practice 8 (vector) A = factor (letters [1: 10]) A [3] = "W" A =. character (A) A [3] = "W" afactor (a) # practice 9 (Data Transmission Input and Output) x = scan () 1.5 2.6 3.7 2.1 12-8.9-4x = C (1.2, 1.5, 2.6, 3.7, 2.1, 12,-8.9,-4) W = read. table (file. choose (), header = T) setwd ("D:/program files/rstudio") (X = rnorm (20) write (x, "d:/test.txt ") y = scan ("D:/test.txt") YY = irisy [,] STR (y) Write. table (Y, "test.txt", row. names = false) W = read. table ("D:/test.txt", header‑t‑str(w‑write.csv (Y, "test.csv" using v‑read.csv ("D:/program files/rstudio/test.csv") STR (v) Data = read. ta Ble ("Clipboard") data # practice 10 (sequence) (Z = seq (-100, length = 100) z = seq (-, Len =) (Z = seq (10,-1,-0.1) (X = rep (, 3) (X = rep )) X = rep (C (), C () XW = C (, x, z) W [3] x = rep () z = 1: 3X + zx * zrev (z) z = C ("no cat", "has", "Nine", "tails ") Z [1] = "no cat" z = 1: 5z [7] = 8zz = nullzz [C (1, 3, 5)] = 1: 3 zrnorm (10) [C ()] Z [-C ()] Z = sample (, 10) zwhich (Z = max (z) which. max (z) # practice 11 (matrix) x = sample (1:100, 12) xall (x> 0) All (X! = 0) Any (x> 0) () [x> 0] diff (x) diff (x, Lag = 2) x = matrix) xx = matrix (, byrow = T) XT (x) x = matrix (sample (, 20) 2 * xx + 5y = matrix (sample (, 20), 5, 4) x + T (y) (Z = x % * % Y) z1 = solve (z) z1 % * % fig (Z1% * % Z, 14) B = solve (z,) B # practice 12 (matrix continue) nrow (x) ncol (X) dim (x) x = matrix (rnorm (24),) x [C (),] X [, C ()] x [] X [x [, 1]> 0, 1] sum (X [, 1]> 0) sum (X [, 1] <= 0) x [,-C (1, 3)] diag (X) diag () diag (5) x [-2,-C ()] x [x [, 1]> 0 & X [, 3] <= 1.51] X [x [, 2]> 0 | x [, 1] <, 1] X [! X [, 2] <1.51, 1] apply (x, 1, mean) apply (x, 2, sum) x = matrix (rnorm (24) X [lower. tri (x)] = 0xx [upper. tri (x)] = 0x # practice 13 (high-dimensional array) x = array (runif (24), C (4, 3, 2) Xis. matrix (x) dim (X) is. matrix (X [1,]) x = array (, C (, 2) x [C (),] x = array, C (, 2) apply (x, 1, mean) apply (x, sum) apply (x, C (), prod) # practice 14 (calculation between a matrix and a vector) x = matrix (, 5, 4) sweep (x,: 5, "*") sweep (x,: 4, "+") x * (x = matrix (sample (, 24), 6, 4) (x1 = Scale (X)) (X2 = Scale (x, scale = false) (X3 = Scale (x, center = false) round (apply (x1, 2, mean) apply (x1, 2, SD) round (apply (X2, 2, mean), 14) apply (X2, 2, SD) round (apply (X3, 2, mean), 14) apply (X3, 2, SD) # Practice 15 (combination of missing values and data) airqualitycomplete. cases (airquality) which (complete. cases (airquality) = false) sum (complete. cases (airquality) Na. omit (airquality) x =; X [12] = 3 (x1 = append (x, 77, after = 5) cbind (, rnorm (5 )) rbind (, rnorm (5) cbind (, 4: 6) rbind (,) (X = rbind (, runif (5), runif (5),) x [! Duplicated (x),] unique (x) # Practice 16 (list) z = List (, Tom = C (, A = List ("r ", letters [1: 5]), W = "Hi! ")) Z [[1] Z [[2] Z $ TZ $ T $ a2z $ t [[3] Z $ T $ W # practice 17 (bar charts and tables) X = scan () 3 3 3 4 1 4 2 1 3 2 5 3 1 2 2 2 2 3 2 2 4 4 3 5 2 2 5 2 barplot (X) table (x) barplot (Table (X)/length (x) # practice 18 (Form A table) library (mass) quineattach (Quine) Table (AGE) Table (sex, age) tab = xtabs (~ Sex + age, Quine) tabunclass (Tab) tapply (days, age, mean) tapply (days, list (sex, age), mean) detach (Quine) # PRACTICE 19 (how to write a function) Ss = function (n = 100) {z = 2; for (I in 2: N) if (any (I % 2 :( i-1) = 0) = false) z = C (Z, I); Return (z)} fix (SS) ss () T1 = sys. time () ss (10000) sys. time ()-t1system. time (SS (10000) # practice 20 (drawing) x = seq (-3, 3, Len = 20) xy = dnorm (x) YW = data. frame (x, y) wpar (mfcol = C (2, 2) plot (Y ~ X, W, main = "normal density function") plot (Y ~ X, W, type = "L", main = "normal density function") plot (Y ~ X, W, type = "O", main = "normal density function") plot (Y ~ X, W, type = "B", main = "normal density function") PAR (mfcol = C () # practice 21 (color and symbol adjustment) plot (7.5, xlim = C (1, 4.5), ylim = C (), type = "N") points (, Rep (, 7 ), cex = seq (3.5, L = 7), Col =, PCH =) text (, Rep (, 7), labels = paste, letters []), cex = seq (, L = 7), Col =) points (, Rep (), PCH = () + 7) text () + 0.25, Rep (), paste () + 7) points (, Rep (), PCH = () + 14) text () + 0.25, Rep (), paste () + 14 ))