+关注继续查看

# R语言快餐教程(1) - 我们开始做统计吧

## R语言简介

R语言是基于S语言的一种开源实现。S语言是贝尔实验室最早开发的一种用于统计的工具，后来成为商业的S-PLUS软件，是一种与SAS和SPSS齐名的统计软件。

R语言的官方网址是：[https://www.r-project.org/]

R语言的一个重要的优势就是R的生态，有大量的高质量的第三方的统计和算法相关的包。

## 在R中活下去

### 获取帮助

help(函数名)

help(sd)

sd                    package:stats                    R Documentation

Standard Deviation

Description:

This function computes the standard deviation of the values in
‘x’.  If ‘na.rm’ is ‘TRUE’ then missing values are removed
before computation proceeds.

Usage:

sd(x, na.rm = FALSE)

Arguments:

x: a numeric vector or an R object which is coercible to one by
‘as.double(x)’.

na.rm: logical.  Should missing values be removed?

Details:

Like ‘var’ this uses denominator n - 1.

The standard deviation of a zero-length vector (after removal of
‘NA’s if ‘na.rm = TRUE’) is not defined and gives an error.
The standard deviation of a length-one vector is ‘NA’.

See Also:

‘var’ for its square, and ‘mad’, the most robust alternative.

Examples:

sd(1:2) ^ 2

### 详细例子

R对于很多函数都有非常详尽的例子，对于图形类的函数，还图文并茂。

> example(sd)

sd> sd(1:2) ^ 2
[1] 0.5

example(hist)

### 保存和读取数据

save(gun_data,file="gun_data.Rdata")

### 安装CRAN上的扩展包

install.packages("fBasics")

library(timeDate)

### 读取csv数据

times,total, copy
1,122.18138504,48.200
2,114.014596224,38.447
3,113.279325008,37.968
4,117.902994871,37.850
5,112.485991001,37.020
6,113.543860197,37.302
7,112.150246143,36.432
8,110.57020092,36.794
9,112.11462307,37.218
10,117.439277172,37.399

gun_data <- read.csv("gun-1128-2.csv",header=T,col.names=c("times","total","copy"))

c()函数用于生成向量。R语言中没有标量，看着来像标量的，其实也是长度为1的向量。

gun_data[,2]
[1] 122.1814 114.0146 113.2793 117.9030 112.4860 113.5439 112.1502 110.5702
[9] 112.1146 117.4393 112.5256 112.2260 109.8838 118.0142 111.8233 112.9887
[17] 113.0641 112.2024 112.2671 111.3121 111.4523 112.3540 111.6928 112.7954
[25] 110.7357 110.3518 115.5703 111.7312 112.3798 111.7919 111.9345 113.4122
[33] 112.2419 111.8948 110.8999 111.8572 112.7399 112.7252 112.6550 111.7474
[41] 109.7888 111.1741 110.6528 112.0334 113.1800 112.9429 111.2413 112.3243
[49] 113.6774 110.8865 112.2398 112.5997 110.9474 111.6045 113.1162 112.7430
[57] 111.4020 113.6355 114.9378 112.1180 111.0081 111.6405 112.9142 110.6213
[65] 112.3076 113.0985 113.9538 112.2582 111.8185 114.2400 114.4118 112.5618
[73] 113.2215 112.0616 114.1094 125.1672 125.7026 114.0470 113.8599 119.5466
[81] 113.7481 118.8992 116.7088 114.9942 115.2779 115.9557 112.9537 113.7493
[89] 114.1442 113.9163 112.8527 119.0420 113.5002 112.9147 113.5229 113.1191
[97] 112.1945 113.7664 111.7049 113.3210

> gun_data[[2]]
[1] 122.1814 114.0146 113.2793 117.9030 112.4860 113.5439 112.1502 110.5702
[9] 112.1146 117.4393 112.5256 112.2260 109.8838 118.0142 111.8233 112.9887
[17] 113.0641 112.2024 112.2671 111.3121 111.4523 112.3540 111.6928 112.7954
[25] 110.7357 110.3518 115.5703 111.7312 112.3798 111.7919 111.9345 113.4122
[33] 112.2419 111.8948 110.8999 111.8572 112.7399 112.7252 112.6550 111.7474
[41] 109.7888 111.1741 110.6528 112.0334 113.1800 112.9429 111.2413 112.3243
[49] 113.6774 110.8865 112.2398 112.5997 110.9474 111.6045 113.1162 112.7430
[57] 111.4020 113.6355 114.9378 112.1180 111.0081 111.6405 112.9142 110.6213
[65] 112.3076 113.0985 113.9538 112.2582 111.8185 114.2400 114.4118 112.5618
[73] 113.2215 112.0616 114.1094 125.1672 125.7026 114.0470 113.8599 119.5466
[81] 113.7481 118.8992 116.7088 114.9942 115.2779 115.9557 112.9537 113.7493
[89] 114.1442 113.9163 112.8527 119.0420 113.5002 112.9147 113.5229 113.1191
[97] 112.1945 113.7664 111.7049 113.3210

> gun_data[["total"]]
[1] 122.1814 114.0146 113.2793 117.9030 112.4860 113.5439 112.1502 110.5702
[9] 112.1146 117.4393 112.5256 112.2260 109.8838 118.0142 111.8233 112.9887
[17] 113.0641 112.2024 112.2671 111.3121 111.4523 112.3540 111.6928 112.7954
[25] 110.7357 110.3518 115.5703 111.7312 112.3798 111.7919 111.9345 113.4122
[33] 112.2419 111.8948 110.8999 111.8572 112.7399 112.7252 112.6550 111.7474
[41] 109.7888 111.1741 110.6528 112.0334 113.1800 112.9429 111.2413 112.3243
[49] 113.6774 110.8865 112.2398 112.5997 110.9474 111.6045 113.1162 112.7430
[57] 111.4020 113.6355 114.9378 112.1180 111.0081 111.6405 112.9142 110.6213
[65] 112.3076 113.0985 113.9538 112.2582 111.8185 114.2400 114.4118 112.5618
[73] 113.2215 112.0616 114.1094 125.1672 125.7026 114.0470 113.8599 119.5466
[81] 113.7481 118.8992 116.7088 114.9942 115.2779 115.9557 112.9537 113.7493
[89] 114.1442 113.9163 112.8527 119.0420 113.5002 112.9147 113.5229 113.1191
[97] 112.1945 113.7664 111.7049 113.3210

### 查询内存中的对象

ls()函数

> ls()
[1] "C."            "C1"            "C2"            "C3"
[5] "Cl"            "clK"           "clS"           "EV"
[9] "gun_1128"      "gun_data"      "gun_data3"     "gun_data4"
[13] "i"             "inst_qq"       "inst_qq_5_1"   "inst_qq_5_4_1"
[17] "op"            "out"           "r"             "R."
[21] "Rc"            "Rp"            "swM"           "total_data"
[25] "x"      

## 我们开始做统计吧

### 均值

R语言中用mean()函数来求均值。

> mean(gun_data3[[2]])
[1] 103.1747
> mean(gun_data4[[2]])
[1] 113.3303

### 中位数

R语言中用median函数求中位数：

> median(gun_data3[[2]])
[1] 101.651
> median(gun_data4[[2]])
[1] 112.7326

### 五数

> fivenum(gun_data3[["total"]])
[1]  98.92649 100.48752 101.65097 105.94518 116.74337
> fivenum(gun_data4[["total"]])
[1] 109.7888 111.8402 112.7326 113.7578 125.7026

> summary(gun_data3[,"total"])
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
98.93  100.50  101.70  103.20  105.80  116.70
> summary(gun_data4[,"total"])
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
109.8   111.8   112.7   113.3   113.8   125.7 

### 方差

> var(gun_data3[,"total"])
[1] 12.70904
> var(gun_data4[,"total"])
[1] 7.397949

### 标准差

> sd(gun_data3[,"total"])
[1] 3.564974
> sd(gun_data4[,"total"])
[1] 2.719917

> sqrt(var(gun_data3[,"total"]))
[1] 3.564974

### 离差

mad(x) = 1/qnorm(3/4) * median(abs(x-median(x)))

### 偏度

> library(timeDate)
> skewness(gun_data3[,2])
[1] 1.109821
attr(,"method")
[1] "moment"
> skewness(gun_data4[,2])
[1] 2.40715
attr(,"method")
[1] "moment"

### 峰度

R中用kurtosis()函数来计算

> kurtosis(gun_data3[,2])
[1] 0.7986081
attr(,"method")
[1] "excess"
> kurtosis(gun_data4[,2])
[1] 7.060265
attr(,"method")
[1] "excess"

10061 0
vscode插件快餐教程(4) - 语言服务器协议lsp

2872 0

1448 0

1:div src src为8位时：ax/src = al余数放在AH中 SRC位16位时：DX:AX/SRC = AX，余数放在DX中 SRC为32位时：EDX:EAX/SRC = EAX,余数放在EDX中   2：IDIV 带符号的除法 A/B的表示如下： mov ax,a c...
580 0

13874 0

1:MUL SRC 无符号数的乘法 SRC是一个乘数，不能是立即数 当SRC为8位时：AX
609 0
+关注
lusing

181

60

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》