R语言编程艺术学习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言编程艺术学习相关的知识,希望对你有一定的参考价值。
###R语言编程艺术=====
####chr1.快速入门======
#交互模式
mean(abs(rnorm(100)))
rnorm(10)
source("z.R")
#批处理模式
setwd("/Users/tang/Downloads/R语言编程艺术/")
#shell命令运行脚本
#R CMD BATCH z.R
data() ##查看内置数据集
mean(Nile)
sd(Nile)
hist(Nile)
hist(Nile, breaks = 12)
q() #linux ctrl-d mac:cmd-d
#赋值运算符:<-,=(特殊情况下失灵)
#超赋值运算符:<<-(在函数内部给全局变量赋值)
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(1,1) ##%*%矩阵乘法
x <- list(u = 2, v = "abc")
x
#列表的常用方法:函数多个返回值打包返回
hist(Nile)
hn <- hist(Nile)
hn
str(hn)
#数据框本质上是列表(每个组件元素个数相等)
d <- data.frame(list(kids = c("jack", "jill"), ages = c(12, 10)))
d$ages
#类
print(hn)
summary(hn)
###chr2.向量======
#主题:循环补齐\筛选\向量化
#变量类型称为模式(mode),程序中查看可用typeof(x)
x <- c(1, 2, 4)
length(x)
#矩阵和数组本质上也是向量(区别是具有额外属性:维度)
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)]
#":"使用时注意运算符优先级问题
i <- 2
1:i-1#:优先级高于-
1:(i-1)#()优先级高于-
# ?Syntax获取优先级的详细说明
seq(from = 12, to = 30, by = 3)
seq(from = 1.1, to = 2, by = 0.1)
#循环时避免使用length,改用seq
#rep(x, times)\rep(x, each)
#all\any:分别报告其参数是否至少有一个还是全部为TRUE
x <- 1:10
all(x > 8)
any(x > 88)
all(x > 88)
all(x > 0)
x > 8
any(x > 8)
##扩展案例:寻找连续出现1的游程
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)会重新分配内存,减慢代码运行速度
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])))
}
#向量化运算符
#向量输入、向量输出
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)
#向量输入,矩阵输出
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(88, NA, 12, 168, 13)
x
mean(x)
mean(x, na.rm = TRUE)
x <- c(88, 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)用于循环中创建向量
z <- NULL
for(i in 1:10) if(i %% 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 筛选
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语言编程艺术学习的主要内容,如果未能解决你的问题,请参考以下文章