# # #R语言编程艺术 =====
# # # #chr1. Quick Start ======
#交互模式
Mean (ABS (Rnorm (100)))
Rnorm (10)
Source ("Z.R")
#批处理模式
SETWD ("/USERS/TANG/DOWNLOADS/R Language Programming Art/")
#shell命令运行脚本
#R CMD BATCH Z.R
Data () # #查看内置数据集
Mean (Nile)
SD (Nile)
hist (Nile)
hist (Nile, breaks = 12)
Q () #linux ctrl-d mac:cmd-d
#赋值运算符: <-,= (failure in special cases)
#超赋值运算符: <<-(Assigning a value to a global variable inside a function)
X <-C (5, 12, 13)
Mode (x)
typeof (X)
Y <-"abc"
Mode (y)
typeof (Y)
U <-paste ("abc", "De", "f", Sep = ":")
V <-unlist (strsplit (u, split = ":"))
M <-Rbind (C (1, 4), C (2, 2))
M
M%*% C (##%*%) matrix multiplication
x <-List (U = 2, v = "abc")
X
#列表的常用方法: Function multiple return values package return
hist (Nile)
HN <-hist (Nile)
Hn
STR (HN)
#数据框本质上是列表 (equal number of elements per component)
D <-data.frame (list (kids = C ("Jack", "Jill"), ages = C (12, 10)))
D$ages
#类
Print (HN)
Summary (HN)
# # #chr2. Vector ======
#主题: cyclic completion \ filtering \ vectorization
#变量类型称为模式 (mode), view available typeof (X) in the program
x <-C (1, 2, 4)
Length (x)
#矩阵和数组本质上也是向量 (The difference is that it has extra attributes: dimension)
M <-Rbind (C (1, 2), C (3, 4))
M + 10:13
M
Y <-vector (mode = "character", length = 2)
Y[1] <-3
Y[2] <-4
typeof (Y)
Y
C (1, 2, 4) + C (6, 0, 9, 20, 22)
#运算符也是一种函数
2 + 3
"+" (2, 3)
x <-C (1, 2, 4)
X + C (5, 0,-1)
X * C (5, 0,-1)
X%*% C (5, 0,-1)
Y <-C (1.2, 3.9, 0.4, 0.12)
Y[c (1, 3)]
Y[2:3]
V <-3:4
Y[V]
#元素重复是允许的
x<-C (4, 2, 17, 5)
Y <-x[c (1, 1, 3)]
Y
#负数下标代表想把相应元素删除
Z <-C (5, 12, 13)
Z[-1]
Z[-1:-2]
Z[1: (Length (z)-1)]
Z[-length (z)]
# ":" Attention to operator precedence issues when using
I <-2
1:i-1#: Priority higher than-
1: (i-1) # () Priority above-
# ? Syntax a detailed description of getting the priority level
Seq (from = a, to = +, by = 3)
SEQ (from = 1.1, to = 2, by = 0.1)
#循环时避免使用length, use SEQ instead
#rep (x, Times) \rep (x, each)
#all \any: Report whether the parameter has at least one or all true
X <-1:10
All (x > 8)
Any (x > 88)
All (x > 88)
All (x > 0)
X > 8
Any (x > 8)
# #扩展案例: Look for a run that appears 1 consecutive runs
Findruns <-function (x, k) {
n <-Length (x)
Runs <-NULL
For (I in 1: (n-k+1)) {
if (All (x[i: (i+k-1)] = = 1)) {
Runs <-c (runs, i)
}
}
Return (runs)
}
Y <-C (1, 0, 0, 1, 1, 1, 0, 1, 1)
Findruns (Y, 3)
Findruns (Y, 2)
Findruns (Y, 6)
#每次调用c (runs, i) re-allocates memory, slowing code speed
Findruns1 <-function (x, k) {
n <-Length (x)
Runs <-vector (length = n)
Count <-0
For (I in 1: (n-k+1)) {
if (All (x[i: (i+k-1)] = = 1)) {
Count <-Count + 1
Runs[count] <-I
}
}
if (Count > 0) {
Runs <-Runs[1:count]
}else runs <-NULL
Return (runs)
}
Y <-C (1, 0, 0, 1, 1, 1, 0, 1, 1)
Findruns1 (Y, 3)
Findruns1 (Y, 2)
Findruns1 (Y, 6)
#预测离散值时间序列
Preda <-function (x, k) {
n <-Length (x)
K2 <-K/2
pred <-vector (length = n-k)
For (I in 1: (N-k)) {
if (SUM (x[i: (i+k-1)]) >= K2) Pred[i] <-0 Else Pred[i] <-0
}
Return (Mean (ABS (pred-x[(k+1): N)))
}
#predb
Predb <-function (x, k) {
n <-Length (x)
K2 <-K/2
Pred <-vector (length = n-k)
SM <-sum (x[1:k])
if (SM >= K2) pred[1] <-1 else pred[1] <-0
if (N-k > 0) {
For (I in 2: (N-k)) {
SM <-SM + x[i+k-1]-x[i-1]
if (SM >= K2) Pred[i] <-1 else pred[i] <-0
}
}
Return (Mean (ABS (pred-x[(k+1): N)))
}
#predc
Predc <-function (x, k) {
n <-Length (x)
K2 <-K/2
pred <-vector (length = n-k)
CSX <-C (0, Cumsum (x))
For (I in 1: (N-k)) {
if (Csx[i+k]-csx[i] >= K2) Pred[i] <-1 else pred[i] <-0
}
Return (Mean (ABS (pred-x[(k+1): N)))
}
#向量化运算符
#向量输入, vector output
U <-C (5, 2, 8)
V <-C (1, 3, 9)
U > V
W <-function (x) return (x + 1)
W (u)
sqrt (1:9)
Y <-C (1.2, 3.9, 0.4)
Z <-round (y)
Z
Round (1.2)
Y <-C (12, 5, 13)
Y + 4
"+" (Y, 4)
F <-function (x, C) return ((x + C) ^2)
F (1:3, 0)
F (1:3, 1:3)
F <-function (x, c) {
if (length (c) > 1) Stop ("Vector c is not allowed")
Return ((x+c) ^2)
}
F (1:3, 1:3)
F (1:3, 0)
#向量输入, matrix output
Z12 <-function (z) return (c (z,z^2))
X <-1:8
Z12 (x)
Matrix (Z12 (x), Ncol = 2)
Sapply (1:8, Z12)
#NA与NULL
#NA的使用
x <-C (D, NA, 12, 168, 13)
X
Mean (x)
Mean (x, na.rm = TRUE)
X <-C (+, NULL, 12, 168, 13)
Mean (x)
X[2]
#NA值模式
X <-C (5, NA, 12)
Mode (x[1])
Mode (x[2])
Y <-C ("abc", "Def", NA)
Mode (y[2])
Mode (Y[3])
#NULL的使用
# (1) for creating vectors in loops
Z <-NULL
For (I-in 1:10) if (i-percent 2 = = 0) z <-c (z, i)
Z
Seq (2, 10, 2)
2 * 1:5
Z <-NA
For (I-in 1:10) if (i%%2 = = 0) z <-c (z, i)
Z
U <-NULL
Length (U)
V <-NA
Length (v)
#2.8 filtering
Z <-C (5, 2,-3, 8)
W <-z[z * z > 8]
W
Z
Z*z > 8
Y <-C (1, 2, 30, 5)
Y[z^2 > 8]
x <-C (1, 3, 8, 2, 20)
X[x > 3] <-0
X
X <-C (6, 1:3, NA, 12)
X
X[x > 5]
Subset (x, x > 5)
Which (Z^2 > 8)
Fisrst1 <-function (x) {
For (I in 1:length (x)) {
if (x[i] = = 1) break
}
return (i)
}
FIRST1A <-function (x) return (which (x = = 1) [1])
#向量化ifelse函数
#ifelse (b, U, v)
X <-1:10
Y <-ifelse (x%%2 = = 0, 5, 12)
Y
X <-C (5, 2, 9, 12)
IfElse (x > 6, 2*x, 3*x)
R Language Programming Art Learning (1)