1.R基本操作
基本操作
1. 安装加载包
install.packages("name")
library ("name", "location")
查看如何使用
help(plot)
2. 数学操作
-
赋值
num = 2
num <- 2 -
运算
a = 7, a + b, a - b, a * b, a / b, a ** b(power), a ^ b(power), a %% b(remainder), sqrt(a), log2(b), log(b,2), exp(a) -
逻辑运算
!,&,|,isTRUE(x == x) -
取小数位round()
round(pi,3) ## [1] 3.142 -
因式分解、最小公分母、最大公约数
library(schoolmath) #导入schoolmath包
prime.factor(8) # 2,2,2
scm(2, scm(3, 4)) # 12
gcd(4,6) # 2
3. 简单数据结构
3.1 向量
-
c():创造向量
a <- c(6, -1, 3, -12, 3.4, 7.05) -
seq(from=, to=, by=):生成相同间隔的向量
e = seq(from=0, to=1, by =.01)
f = seq(1,100,20)
g = seq(1,15, length.out=8) -
rep(x, times=) or rep(x, each=):生成有重复数据的向量
rep(c(1,2,3), times=2) #[1] 1 2 3 1 2 3
rep(c(1,2,3), each=2) #[1] 1 1 2 2 3 3 -
索引
注意:从1开始计数,负索引表示去掉这个数,包左也包右 a <- c(6, -1, 3, -12, 3.4, 7.05)
a[1] # [1] 6
a[-1] # [1] -1.00 3.00 -12.00 3.40 7.05
b[2:4] # [1] "car" "apple" "banana" ( from 2nd to 4th element) -
向量的函数
length(n1):长度
mean(n1): 均值
median(n1):中位数
min(n1):最小值
max(n1):最大值
summary(n1): min, max, mean, med, quartile information -
向量间运算
注意:[]中的运算是选择的意思(只挑选满足条件的元素),向量直接参与运算是每个元素参与运算
注意:不同的向量间可以共享索引,参与混合运算
n1 * n2:每个元素参与运算
a[a>5] # [1] 6.00 7.05
o[o%%2==0] # 求o中的even偶数 o[is.even(o)] # 求o中偶数 a>5 # [1] TRUE FALSE FALSE FALSE FALSE TRUE
y[x==6] #shows elements of x whose condition indexes in x were true -
schoolmath学习
x[is.prim(x)] #求出x中为素数的数
primes()[1:50] # 求前50个素数
3.2 矩阵
-
matrix():构造矩阵
matrix_name <- matrix(vector, nrow=r, ncol=c, byrow=FALSE,dimnames=list(char_vector_rownames, char_vector_colnames))
X2 = rbind(c(1,2,4,5), c(3,4,9,1)) #binds by row
X3 = cbind(c(1,2,4), c(3,5,1), c(5,6,11)) #binds by columnvalues <- c(1,34,5,99)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
Y <- matrix(values, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames)) -
矩阵索引:只取用'c',连着取用':',不想要用负号'-'
X[3,] # 3rd row of matrix
X[2:3,1:3] # rows 2,3 of columns 1,2,3
X[, c(2,4)] # 2nd and 4th column
X[,-1] -
矩阵运算
注意:[]中的运算是选择的意思(只挑选满足条件的元素),矩阵直接参与运算是每个元素参与运算 A * B # 每个元素相乘
A %*% B # 矩阵相乘
A != B # 每个元素相比A[A%%2==0] # [1] 10 2 8
A%%2==0 [,1] [,2] [,3]
[1,] FALSE TRUE FALSE
[2,] FALSE TRUE FALSE
[3,] TRUE FALSE FALSE -
矩阵函数
mean(A) # calculates mean across all values
colMeans(A) # mean by column
rowMeans(A) # mean by row
sd(A) # standard deviation across all values
length(A) #total number of elements
ncol(C) # number of columns
nrow(C) # number of rows
rowSums(C) # adds elements per row
colSums(C) # adds elements per column -
矩阵操作
diag(A) #矩阵A的对角矩阵
t(A) #矩阵A的转置
det(A) #A的行列式
矩阵的逆:
g_aux= diag(rep(1,times=3)) #A是3行3列矩阵
g=solve(A,g_aux) -
练习题补充sum、which、colSums使用
G=c(0,1,0,1,1,0)
sum(G==1) #G中为1的个数avgs = c(45.6, 70.8, 74.0, 87.2, 60.8, 61.2)
sum(avgs>=55 & avgs<=75)which.min(abs(apply(N,2, max)-apply(N,2,min)))
求矩阵N中的每一列的最大最小值之差的最小值所在的列,结果为3.colSums(N>=70)
注意:如果colSums(A)其中A是boolean矩阵的话,返回的是每一列中为TRUE的个数通过向量的索引来选择矩阵
N = rbind(c(32, 52, 50, 44, 50),
c(88, 67, 59, 70, 70),
c(78, 77, 68, 67, 80),
c(89, 90, 81, 89, 87),
c(61, 65, 50, 78, 50),
c(67, 68, 65, 40, 66))
G=c(0,1,0,1,1,0)
M=N[G==0,]
which.max(colSums(M<50)
G[which.max(N[,4])
3.3 数组Arrays
注意:数组跟矩阵类似,只是数组可以超过2维
- array():创建数组
ar1 = array(1:27, dim=c(3,3,3))
ar2 = array(sample.int(100,27), dim=c(3,3,3)) # sample.int(a,b) generates b integer nums in the range [0,a]
4. 画简单图plot()
a = seq(1,15)
c = 2*a+1
b = a + 2
plot(a, c, type = 'l', col="red", main = "A Title", xlab = "X Label", ylab = "Y Label")
lines(a, b, col='blue')

-
三角函数
sin(x)
cos(x) -
The cos function between -4π and 4π with red markers every 0.01 values
x1 = seq(-4pi,4pi,by=0.01)
y1 = cos(x1)
plot(x1,y1,col="red",pch="*", main = "Cos(x) between -4Pi and 4Pi")![]()
-
Create a 2x2 plot that shows the following functions in each quadrant. Each plot should: a) be shown between the -5 and 5 at intervals of 0.5 and b) Have sensible titles and axes labels:
a. y = x
b. y = x
c. y = x
d. y = xsinx= seq(-5,5, by=0.5)
siny1 = sinx
siny2= sinx^2
siny3= sinx^3
siny4= sinx^4
par(mfrow=c(2,2))
plot(sinx,siny1,main= "y=x", xlab = "x axis", ylab = "y axis")
plot(sinx,siny2,main= "y=x^2", xlab = "x axis", ylab = "y axis")
plot(sinx,siny3,main= "y=x^3", xlab = "x axis", ylab = "y axis")
plot(sinx,siny4,main= "y=x^4", xlab = "x axis", ylab = "y axis")![]()



浙公网安备 33010602011771号