0
点赞
收藏
分享

微信扫一扫

sqllab第二关通关笔记

金穗_ec4b 03-13 09:30 阅读 2

往期R统计学文章:

R统计学1 - 基础操作入门问题1-20

21. 如何对矩阵按行 (列) 作计算?

使用函数 apply()

vec = 1:20
# 转换为矩阵
mat = matrix (vec , ncol=4)
# [,1] [,2] [,3] [,4]
# [1,]    1    6   11   16
# [2,]    2    7   12   17
# [3,]    3    8   13   18
# [4,]    4    9   14   19
# [5,]    5   10   15   20

cumsum(vec)
# 返回一个向量,其元素是参数元素的累积和。
#  [1]   1   3   6  10  15  21  28  36  45  55  66  78  91 105 120 136 153 171 190 210

# 对列进行计算
apply (mat, 2 , cumsum)
# [,1] [,2] [,3] [,4]
# [1,]    1    6   11   16
# [2,]    3   13   23   33
# [3,]    6   21   36   51
# [4,]   10   30   50   70
# [5,]   15   40   65   90

22.一组数中随机抽取数据?

n <- 1000

# 随机抽取1000个数,取值范围-1至1
x <- sample(c(-1,1), n, replace = T)

# 绘制折线图
plot(cumsum(x), type = "l", main = "Sums")

# 模拟生成正态分布数据
# rnorm(n=100 , mean=0, sd=1)

折线图

23. 判断数据框的列是否为数字?

sapply(dataframe, is.numeric)

24.如何将数据标准化?

使用scale函数。

x <- c(rnorm(100), 2 * rnorm(30))
m <- scale(x, scale = F)
n <- scale(x, center = F)

25.如何获取分位数?

x <- c (1 , 4 , 6 , 17 , 50 , 51 , 70 , 100)
quantile(x)
#    0%    25%    50%    75%   100% 
#  1.00   5.50  33.50  55.75 100.00

26.如何生成对角矩阵?

对一个向量使用 diag() 函数,得到对角线元素为向量的对角矩阵.

diag(3)
# [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    0    1    0
# [3,]    0    0    1

27. 如何构造上(下)三角矩阵?

使用lower.tri() 和 upper.tri()函数。

# 下三角矩阵
Rmat <- matrix(1:16, 4:4)
Rmat[upper.tri(Rmat)] <- 0
Rmat
# [,1] [,2] [,3] [,4]
# [1,]    1    0    0    0
# [2,]    2    6    0    0
# [3,]    3    7   11    0
# [4,]    4    8   12   16

# 上三角矩阵
Rmat <- matrix(1:16, 4:4)
Rmat[lower.tri(Rmat)] <- 0
Rmat
# [,1] [,2] [,3] [,4]
# [1,]    1    5    9   13
# [2,]    0    6   10   14
# [3,]    0    0   11   15
# [4,]    0    0    0   16

28. 如何求矩阵各行 (列) 的均值?

使用 apply() 函数或colMeans() 函数。

n <- 5
m <- 4
# 构造5行,4列矩阵
mat <- matrix(1:m*n, m, n)
mat
# [,1] [,2] [,3] [,4] [,5]
# [1,]    5    5    5    5    5
# [2,]   10   10   10   10   10
# [3,]   15   15   15   15   15
# [4,]   20   20   20   20   20

mat_mean <- matrix(apply(mat, 2, mean), m, n, by=T)
# 或 mat_mean <- matrix(colMeans(mat), m, n, by=T)

mat_mean
# [,1] [,2] [,3] [,4] [,5]
# [1,] 12.5 12.5 12.5 12.5 12.5
# [2,] 12.5 12.5 12.5 12.5 12.5
# [3,] 12.5 12.5 12.5 12.5 12.5
# [4,] 12.5 12.5 12.5 12.5 12.5

29 如何求一元方程的根?

使用uniroot()函数,该函数基于二分法计算方程根,初始区间不满足求根条件,则会报错。

f <- function(x)x^3 - 2*x -1
uniroot(f, c(0,2))
# $root
# [1] 1.618018
# 
# $f.root
# [1] -9.17404e-05
# 
# $iter
# [1] 6
# 
# $init.it
# [1] NA
# 
# $estim.prec
# [1] 6.103516e-05

30. 如何在 R 里面求(偏)导数?

使用函数D()

f1 <- expression(sin(x)*x)
D(f1,"x")
# cos(x) * x + sin(x)

f2 <- expression(x^2*y + y^2)
D(f2,"y")
# x^2 + 2 * y

31. 如何在 R 中计算高斯(正态)分布的概率计算?

如已知 X˜N(3, 1),计算P(2 ≤ X ≤ 5)。

# 利用正态分布的累积分布函数 pnorm
pnorm (5 ,3 , 1 ) − pnorm (2 ,3 ,1 )
# 计算结果为 0.8185946,即下图中阴影的面积。

32. R如何在保存文件时用变量替换文件名内容?

使用 paste() 函数。

save_string = "test"
for(var in range(1,3)){
	# paste("File_", var, ".txt", sep = "")为组合的文件名
  write.table(save_string , paste("File_", var, ".txt", sep = ""))
}

33. 如何在R中使用正则表达式?

使用 grep() 函数。

index <- grep("J.", month.abb)
# [1] 1 6 7
month.abb[index]
# [1] "Jan" "Jun" "Jul"

34. R语言如何截取字符串?

使用 substr() 函数。

str <- "abcdefg"
substr(str, 2, 4)
# [1] "bcd"
substring(str, 1:6, 1:3)
# [1] "a" "b" "c" ""  ""  ""

35. R语言如何对日期进行算术运算?

使用 difftime(arg) 函数, arg支持“auto”, “secs”, “mins”, “hours”, “days”, “weeks”参数。

d1 <- c("24/02/01")
d2 <- c("24/03/01")

D1 <-as.Date(d1, "%y/%m/%d")
D2 <-as.Date(d2, "%y/%m/%d")

difftime(D2, D1, units = "days")
# Time difference of 29 days

difftime(D2, D1, units = "weeks")
# Time difference of 4.142857 weeks

36. R语言如何对系统时间进行格式化输出?

使用 format() 函数。

format((Sys.Date()), format="%A, %d %B %Y")

37. R语言如何在同一画面画出多张图?

推荐使用 layout() 函数。

layout(matrix(c(1, 1, 1,
                2, 3, 4), nr=2, byrow=T))
hist(rnorm(10, 0, 1), col = "VioletRed")
hist(rnorm(10, 0, 1), col = "VioletRed")
hist(rnorm(10, 0, 1), col = "VioletRed")
hist(rnorm(10, 0, 1), col = "VioletRed")

多张图布局效果

38. R语言如何设置图形边缘大小?

修改绘图参数 par(mar = c(bottom, left, top, right))

# 默认矩阵
par(mar = c(5, 4, 4, 2) + 0.1)

39. R语言 常用的 pch 符号都有哪些?

pch 是 plotting character 的缩写。pch 符号可以使用 “0 : 25” 来表示 26 个标识(参
看右图 “pch 符号”)。当然符号也可以使用#, %, ∗, |, +, −, ., o, O。值得注意的是,21 : 25
这几个符号可以在 points 函数使用不同的颜色填充(bg= 参数)。

pch编号对应图形

op <- par(bg = "light blue")
x <- seq(0, 2*pi, len=51)

plot(x, sin(x), type="o", bg=par("bg"))
points(x, sin(x), pch=21, cex=1.5, bg="red")

绘制点图

40. R语言如何给图形加上图例?

绘制图形后,使用 legend函数。

# 查看自带isis数据集
head(iris)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa
with(iris, plot(Sepal.Length, Sepal.Width, 
                pch=as.numeric(Species), cex=1.2))

table(iris$Species)
# setosa versicolor  virginica 
# 50         50         50 

legend(6.1, 4.1, c("setosa", "versicolor", "virginica"),
       cex=1.5, pch=1:3)

图例

举报

相关推荐

0 条评论