数据类型
character:字符型
numeric:数值型,指实数或小数
integer:整型
complex:复数型
logical:逻辑型
数据结构
数据分析的对象
数值变量
分类变量(有序、无序)
向量、因子
矩阵、数据框
数组、列表
向量
创建向量
c()
x = c(1,2,3,4,5)
冒号操作符:
x = 1:10
seq()
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),length.out = NULL, along.with = NULL, ...)
rep()
rep(x, times = 1, length.out = NA, each = 1)
vector()、numeric()、integer()、character()、logical()、complex()
vector(mode = "数据类型", length = 0)
numeric(length = 0)
integer(length = 0)
character(length = 0)
logical(length = 0)
complex(length.out = 0, real = numeric(), imaginary = numeric(),modulus = 1, argument = 0)
示例:
> (x = c(10,11,13,15))
[1] 10 11 13 15
> (x=1:10)
[1] 1 2 3 4 5 6 7 8 9 10
> (x = seq(5,by=2,length=4))
[1] 5 7 9 11
> (y = seq(10,by=3,along.with = x))
[1] 10 13 16 19
> (y1 = rep(x,times=3))
[1] 5 7 9 11 5 7 9 11 5 7 9 11
> (y2 = rep(x,each=3))
[1] 5 5 5 7 7 7 9 9 9 11 11 11
> (y3 = rep(x,times=2,each=3,length=17))
[1] 5 5 5 7 7 7 9 9 9 11 11 11 5 5 5 7 7
> (x=vector(mode='logical',length=5))
[1] FALSE FALSE FALSE FALSE FALSE
> (y1[1:5]);(y1[c(1,2,3,4,5)]);(y1[seq(1,5,1)])
[1] 5 7 9 11 5
[1] 5 7 9 11 5
[1] 5 7 9 11 5
> (y1[-5:-1])
[1] 7 9 11 5 7 9 11
> (which(y1>5))
[1] 2 3 4 6 7 8 10 11 12
> (y1[which(y1>5)])
[1] 7 9 11 7 9 11 7 9 11
> length(y1)
[1] 12
> mode(y1);class(y1)
[1] "numeric"
[1] "numeric"
提取子集
下标运算符[]
数字下标(正数、复数)
which()函数返回的是符合条件的下标
y1 = c(1,3,5,6,7,8)
which(y1>5)
[1]4,5,6
(y1[which(y1>5)])
[1]6,7,8
查看属性
长度length()
类型mode()、class()
mode()查看向量中每一个元素的数据类型
class()查看整个向量的数据类型
这两个方法在数据框中才能看出差异
因子
因子--分类变量
级别--类别
注:名义变量和有序变量可以使用因子来表示。
创建因子
factor()
factor(x=charactor(), levels, labels=levels, exclude = NA, ordered = is.ordered(x), namax = NA)
x 为创建因子的数据,是一个向量;levels:因子数据的水平,默认是x中不重复的值;
labels:标识某水平的名称,与水平一一对应,以方便识别,默认取levels的值;
exclude:从x中剔除的水平值,默认为NA值;
ordered:逻辑值,因子水平是否有顺序(编码次序),若有取TRUE,否则取FALSE;
nmax:水平个数的限制。
(x = factor(c('a','a','b','o','ab','ab'),levels = c('a','b','o','ab')))
创建factor因子,以levels排列如果不定义levels会按照字典顺序进行排列
gl()
gl()函数用于定义有规律的因子向量,其语法格式如下:
gl(n, k, length = n*k, labels = 1:n, ordered = FALSE)
n: 正整数,表示因子的水平个数; k:正整数,表示每个水平重复的次数; length: 正整数,表示因子向量的长度,默认为n*k
labels: 表示因子水平的名称,默认值为1:n; ordered: 逻辑变量,表示因子水平是否是有次序的,默认值为FALSE
使用unclass()可以查看因子排列次序的下标和排列水平
查看属性
mode()、class()
类型转换
安全级别:字符串>数字>逻辑
隐式
显示 as 家族
示例:
> (x=factor(c('a','a','b','o','ab','ab')))
[1] a a b o ab ab
Levels: a ab b o
> unclass(x)
[1] 1 1 3 4 2 2
attr(,"levels")
[1] "a" "ab" "b" "o"
> (x=factor(c('a','a','b','o','ab','ab'),levels=c('a','b','o','ab')))
[1] a a b o ab ab
Levels: a b o ab
> unclass(x)
[1] 1 1 2 3 4 4
attr(,"levels")
[1] "a" "b" "o" "ab"
> (x=c(rep('a',3),rep('b',4)))
[1] "a" "a" "a" "b" "b" "b" "b"
> (xf=factor(x,levels=c('b','a')))
[1] a a a b b b b
Levels: b a
> unclass(xf)
[1] 2 2 2 1 1 1 1
attr(,"levels")
[1] "b" "a"
> (f2=gl(4,3,labels=c('blue','red','green','yellow')))
[1] blue blue blue red red red green green green yellow yellow
[12] yellow
Levels: blue red green yellow
> unclass(f2)
[1] 1 1 1 2 2 2 3 3 3 4 4 4
attr(,"levels")
[1] "blue" "red" "green" "yellow"
> mode(f2);class(f2)
[1] "numeric"
[1] "factor"
> (x=c(1,T,'abc'))
[1] "1" "TRUE" "abc"
> mode(x);class(x)
[1] "character"
[1] "character"
> x=1:4
> (y=as.character(x))
[1] "1" "2" "3" "4"
矩阵
创建矩阵
matrix()
修改向量dim属性
rbind、cbind
操作
提取子集[,]
行、列命名
缺失值处理
矩阵运算
加减
转置t()
数乘、对应元素相乘、矩阵乘法 %*%
对角阵、单位阵 diag()
特征值、特征向量 eigen()
求逆、方程组的解 solve()
示例:
> (x=matrix(nrow=2,ncol=3))
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
> (x=matrix(1:18,nrow=3,ncol=6))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 7 10 13 16
[2,] 2 5 8 11 14 17
[3,] 3 6 9 12 15 18
> (x=matrix(1:18,nrow=3,ncol=6,byrow=T))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6
[2,] 7 8 9 10 11 12
[3,] 13 14 15 16 17 18
> (x=1:18)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
> dim(x)=c(3,6);x
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 7 10 13 16
[2,] 2 5 8 11 14 17
[3,] 3 6 9 12 15 18
> (x=1:10)
[1] 1 2 3 4 5 6 7 8 9 10
> (y=11:20)
[1] 11 12 13 14 15 16 17 18 19 20
> (z=21:30)
[1] 21 22 23 24 25 26 27 28 29 30
> (m1=rbind(x,y,z))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
x 1 2 3 4 5 6 7 8 9 10
y 11 12 13 14 15 16 17 18 19 20
z 21 22 23 24 25 26 27 28 29 30
> (m2=cbind(x,y,z))
x y z
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25
[6,] 6 16 26
[7,] 7 17 27
[8,] 8 18 28
[9,] 9 19 29
[10,] 10 20 30
> mode(m1);class(m1)
[1] "numeric"
[1] "matrix"
> (x=matrix(1:18,nrow=3,ncol=6,byrow=T))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6
[2,] 7 8 9 10 11 12
[3,] 13 14 15 16 17 18
> x[2,3]
[1] 9
> x[2,]
[1] 7 8 9 10 11 12
> x[,3]
[1] 3 9 15
> x[,c(2,3,5)]
[,1] [,2] [,3]
[1,] 2 3 5
[2,] 8 9 11
[3,] 14 15 17
> x[c(1,2),]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6
[2,] 7 8 9 10 11 12
> x[c(1,2),c(2,3,4)]
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 8 9 10
> colnames(x)=c('C1','C2','C3','C4','C5','C6')
> rownames(x)=c('R1','R2','R3')
> x
C1 C2 C3 C4 C5 C6
R1 1 2 3 4 5 6
R2 7 8 9 10 11 12
R3 13 14 15 16 17 18
> x['R1','C1']
[1] 1
> x[c('R2','R3'),c('C2','C4')]
C2 C4
R2 8 10
R3 14 16
> x[2,3]=NA;x # 缺失值处理
C1 C2 C3 C4 C5 C6
R1 1 2 3 4 5 6
R2 7 8 NA 10 11 12
R3 13 14 15 16 17 18
> is.na(x)
C1 C2 C3 C4 C5 C6
R1 FALSE FALSE FALSE FALSE FALSE FALSE
R2 FALSE FALSE TRUE FALSE FALSE FALSE
R3 FALSE FALSE FALSE FALSE FALSE FALSE
> x[is.na(x)]=0
> x
C1 C2 C3 C4 C5 C6
R1 1 2 3 4 5 6
R2 7 8 0 10 11 12
R3 13 14 15 16 17 18
#矩阵的运算
> (m1=matrix(1:12,nrow=3,ncol=4))
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> (m2=matrix(13:24,nrow=3,ncol=4))
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
> m1;m2
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
> m1+10
[,1] [,2] [,3] [,4]
[1,] 11 14 17 20
[2,] 12 15 18 21
[3,] 13 16 19 22
> m1+m2
[,1] [,2] [,3] [,4]
[1,] 14 20 26 32
[2,] 16 22 28 34
[3,] 18 24 30 36
> m1*10
[,1] [,2] [,3] [,4]
[1,] 10 40 70 100
[2,] 20 50 80 110
[3,] 30 60 90 120
> m1*m2
[,1] [,2] [,3] [,4]
[1,] 13 64 133 220
[2,] 28 85 160 253
[3,] 45 108 189 288
> m1%*%t(m2)
[,1] [,2] [,3]
[1,] 430 452 474
[2,] 500 526 552
[3,] 570 600 630
> diag(4)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
> diag(c(1,2,3,4,5))
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5
> (a=matrix(1:16,4,4))
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> diag(a)
[1] 1 6 11 16
> (x=matrix(rnorm(16,10,2),4,4))
[,1] [,2] [,3] [,4]
[1,] 11.280838 8.456684 8.421075 12.01786
[2,] 10.476397 10.405870 7.822997 10.55659
[3,] 8.212324 11.106933 10.999944 11.52484
[4,] 9.574400 9.899439 11.447803 11.11046
> (y=solve(x)) # solve() 求矩阵的逆
[,1] [,2] [,3] [,4]
[1,] -0.06916138 0.2170922 -0.4446942 0.3298198
[2,] -0.26497001 0.3126680 0.1083653 -0.1228778
[3,] -0.11798694 -0.1270152 -0.1653525 0.4198259
[4,] 0.41725771 -0.3347945 0.4570331 -0.5173045
> x%*%y
[,1] [,2] [,3] [,4]
[1,] 1 0.000000e+00 0 0.000000e+00
[2,] 0 1.000000e+00 0 -8.881784e-16
[3,] 0 4.440892e-16 1 -8.881784e-16
[4,] 0 -4.440892e-16 0 1.000000e+00
> b=1:4
> solve(x,b)
[1] 0.3502198 0.1939507 0.8112288 -0.9504500
数据框
创建数据框
read.csv()
data.frame()
常用操作
$
attach、detach、with、within
添加新列
subset
示例:
> (a=data.frame(fx=rnorm(10,10,2),fy=runif(10,10,20),fmonth=1:10))
fx fy fmonth
1 7.134070 10.54710 1
2 7.923362 14.87347 2
3 8.942251 17.08393 3
4 12.522647 10.93777 4
5 12.183354 11.81763 5
6 6.366592 14.92479 6
7 7.217653 17.77192 7
8 11.293367 18.29719 8
9 9.754869 15.29030 9
10 9.159064 15.99139 10
> a[1,2]
[1] 10.5471
> a[1,]
fx fy fmonth
1 7.13407 10.5471 1
> a[,2]
[1] 10.54710 14.87347 17.08393 10.93777 11.81763 14.92479 17.77192 18.29719
[9] 15.29030 15.99139
> y1=a[1];y2=a[[1]]
> mode(y1);class(y1)
[1] "list"
[1] "data.frame"
> mode(y2);class(y2)
[1] "numeric"
[1] "numeric"
> y1=a['fx'];y2=a[['fx']]
> mode(y1);class(y1)
[1] "list"
[1] "data.frame"
> mode(y2);class(y2)
[1] "numeric"
[1] "numeric"
> y1
fx
1 7.134070
2 7.923362
3 8.942251
4 12.522647
5 12.183354
6 6.366592
7 7.217653
8 11.293367
9 9.754869
10 9.159064
> y2
[1] 7.134070 7.923362 8.942251 12.522647 12.183354 6.366592 7.217653 11.293367
[9] 9.754869 9.159064
> y3=a$fx
> mode(y3);class(y3)
[1] "numeric"
[1] "numeric"
> search()
[1] ".GlobalEnv" "a" "a" "tools:rstudio"
[5] "package:stats" "package:graphics" "package:grDevices" "package:utils"
[9] "package:datasets" "package:methods" "Autoloads" "package:base"
> attach(a)
The following objects are masked from a (pos = 3):
fmonth, fx, fy
The following objects are masked from a (pos = 4):
fmonth, fx, fy
> search()
[1] ".GlobalEnv" "a" "a" "a"
[5] "tools:rstudio" "package:stats" "package:graphics" "package:grDevices"
[9] "package:utils" "package:datasets" "package:methods" "Autoloads"
[13] "package:base"
> detach(a)
> search()
[1] ".GlobalEnv" "a" "a" "tools:rstudio"
[5] "package:stats" "package:graphics" "package:grDevices" "package:utils"
[9] "package:datasets" "package:methods" "Autoloads" "package:base"
> detach(a)
> detach(a)
> search()
[1] ".GlobalEnv" "tools:rstudio" "package:stats" "package:graphics"
[5] "package:grDevices" "package:utils" "package:datasets" "package:methods"
[9] "Autoloads" "package:base"
# 注:attach和detach要配对使用
> with(a,{fx=rnorm(10,10,20)})
> a
fx fy fmonth
1 7.134070 10.54710 1
2 7.923362 14.87347 2
3 8.942251 17.08393 3
4 12.522647 10.93777 4
5 12.183354 11.81763 5
6 6.366592 14.92479 6
7 7.217653 17.77192 7
8 11.293367 18.29719 8
9 9.754869 15.29030 9
10 9.159064 15.99139 10
> a=within(a,{fx=1:10
+ abc=2:11})
> a
fx fy fmonth abc
1 1 10.54710 1 2
2 2 14.87347 2 3
3 3 17.08393 3 4
4 4 10.93777 4 5
5 5 11.81763 5 6
6 6 14.92479 6 7
7 7 17.77192 7 8
8 8 18.29719 8 9
9 9 15.29030 9 10
10 10 15.99139 10 11
> a$fz=a$fx+a$fy
> a
fx fy fmonth abc fz
1 1 10.54710 1 2 11.54710
2 2 14.87347 2 3 16.87347
3 3 17.08393 3 4 20.08393
4 4 10.93777 4 5 14.93777
5 5 11.81763 5 6 16.81763
6 6 14.92479 6 7 20.92479
7 7 17.77192 7 8 24.77192
8 8 18.29719 8 9 26.29719
9 9 15.29030 9 10 24.29030
10 10 15.99139 10 11 25.99139
> a=transform(a,fw=a$fx+a$fy)
> a
fx fy fmonth abc fz fw
1 1 10.54710 1 2 11.54710 11.54710
2 2 14.87347 2 3 16.87347 16.87347
3 3 17.08393 3 4 20.08393 20.08393
4 4 10.93777 4 5 14.93777 14.93777
5 5 11.81763 5 6 16.81763 16.81763
6 6 14.92479 6 7 20.92479 20.92479
7 7 17.77192 7 8 24.77192 24.77192
8 8 18.29719 8 9 26.29719 26.29719
9 9 15.29030 9 10 24.29030 24.29030
10 10 15.99139 10 11 25.99139 25.99139
> b=subset(a,fx>1&fmonth==8,select = c(fx,fmonth))
> b
fx fmonth
8 8 8
> b=edit(a)
> b
fx fy fmonth abc fz fw
1 1 10.54710 1 2 11.54710 11.54710
2 2 14.87347 2 3 16.87347 16.87347
3 3 17.08393 3 4 20.08393 20.08393
4 4 10.93777 4 5 14.93777 14.93777
5 5 11.81763 5 6 16.81763 16.81763
6 6 14.92479 6 7 20.92479 20.92479
7 7 17.77192 7 8 24.77192 24.77192
8 22 18.29719 8 9 26.29719 26.29719
9 9 15.29030 9 10 24.29030 24.29030
10 10 15.99139 10 11 25.99139 25.99139
> a
fx fy fmonth abc fz fw
1 1 10.54710 1 2 11.54710 11.54710
2 2 14.87347 2 3 16.87347 16.87347
3 3 17.08393 3 4 20.08393 20.08393
4 4 10.93777 4 5 14.93777 14.93777
5 5 11.81763 5 6 16.81763 16.81763
6 6 14.92479 6 7 20.92479 20.92479
7 7 17.77192 7 8 24.77192 24.77192
8 8 18.29719 8 9 26.29719 26.29719
9 9 15.29030 9 10 24.29030 24.29030
10 10 15.99139 10 11 25.99139 25.99139
> fix(a)
> a
fx fy fmonth abc fz fw
1 1 10.54710 1 2 11.54710 11.54710
2 2 14.87347 2 3 16.87347 16.87347
3 3 17.08393 3 4 20.08393 20.08393
4 4 10.93777 4 5 14.93777 14.93777
5 5 11.81763 5 6 16.81763 16.81763
6 6 14.92479 6 7 20.92479 20.92479
7 7 17.77192 7 8 24.77192 24.77192
8 8 18.29719 8 9 26.29719 26.29719
9 9 15.29030 9 10 24.29030 24.29030
10 55 15.99139 10 11 25.99139 25.99139
列表(通常作为函数的返回值时使用列表)
创建列表
list()
提取子集
[]
[[]]
示例:
> a = list(x=1:10,y=matrix(1:16,4,4),z=data.frame())
> a
$`x`
[1] 1 2 3 4 5 6 7 8 9 10
$y
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
$z
data frame with 0 columns and 0 rows
> names(a)
[1] "x" "y" "z"
> c = a[1]
> c
$`x`
[1] 1 2 3 4 5 6 7 8 9 10
> is.list(a)
[1] TRUE
> is.data.frame(a)
[1] FALSE
数组
矩阵的延伸
数据结构
如何判断对象类型
is.list()
is.data.frame()
str()
类型转换
as.matrix()
as.data.frame()
分支结构
if——else
if(condition){
...
}else{
...
}
Ifelse()函数
ifelse(b,u,v)
循环结构
for
for (n in x){
...
}
while
while (condition){
...
}
repeat
repeat{
...
break
}
break、next
示例:
> for (x in 1:5){print(x^2)}
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
> x=1:10
> y=ifelse(x%%2==0,'A','B')
> y
[1] "B" "A" "B" "A" "B" "A" "B" "A" "B" "A"
> i=1
> while(i<6){
+ print(i^2)
+ i=i+1
+ }
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
> repeat{
+ print(i^2)
+ i=i+1
+ if (i>5) break
+ }
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
函数
Function
myfun = function(par1,par2...){
... ...
}
不加括号得函数名,page
source
source('E:/Rtest/06.function.r')
示例:
> myadd = function(a,b,c){return (a+b+c)}
> myadd
function(a,b,c){return (a+b+c)}
> mystat = function(x,na.omit=FALSE){
+ if(na.omit){
+ x=x[!is.na(x)]
+ }
+ m=mean(x)
+ n=length(x)
+ s=sd(x)
+ skew=sum((x-m)^3/s^3)/n
+ return (c(n=n,mean=m,stdev=s,skew=skew))
+ }
# 把上面的函数存成文件‘G:\work\R\06.function.r’ 注:source中的文件路径要用反斜杠
> source('G:/work/R/06.function.r')
> x=rnorm(100,10,3)
> mystat(x)
n mean stdev skew
100.0000000 9.9993176 2.9258409 -0.2116254
向量化计算和apply家族
向量化计算
+-*/
> < !=
*apply家族
apply
lapply、sapply
mapply、tapply
示例:
> BMI <- data.frame(
+ gender = c('Male','Male','Female'),
+ height = c(152,171.5,165),
+ weight = c(81,93,78),
+ Age = c(42,38,26)
+ )
> print(BMI)
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
> x = data.frame(
+ pv = rnorm(100,20,3)
+ ,uv = rnorm(100,40,4),
+ ip = runif(100,40,50)
+ )
> print(x)
pv uv ip
1 23.05832 42.35293 42.16920
2 20.79374 41.92981 49.30058
3 16.05940 41.68341 45.95167
4 20.42891 39.97196 45.96870
5 20.26306 39.17139 49.54307
6 21.31583 38.62511 48.46164
7 18.53188 48.15676 42.28585
8 17.41477 36.77021 41.20942
9 21.98838 34.75892 47.62961
10 15.95987 40.15233 43.97913
11 24.82541 33.62857 44.19524
12 21.39504 37.40006 44.18872
13 23.61316 40.99605 45.50902
14 23.06008 46.04913 43.73322
15 14.15063 38.57941 48.07345
16 16.16929 37.67618 49.73366
17 20.02801 47.40113 43.30028
18 18.47186 40.02153 41.30493
19 17.33759 34.57687 43.62964
20 19.87053 40.71835 43.94486
21 20.00110 41.19260 45.43191
22 18.92471 34.90190 48.19335
23 22.81660 41.20697 42.04338
24 23.49422 44.25056 43.18565
25 18.49218 44.66196 46.07772
26 25.56192 37.74127 47.38519
27 20.15180 41.17829 44.06321
28 25.32429 41.82157 40.39736
29 13.85592 43.55658 48.39827
30 15.54484 36.49160 45.65504
31 20.76963 40.52797 41.73651
32 20.83585 42.08224 44.31869
33 17.88391 33.61450 42.91146
34 20.77511 41.28516 44.02032
35 17.29380 40.16302 40.80999
36 15.97153 37.58540 48.55930
37 24.28821 41.76062 45.03541
38 20.83089 45.17579 42.83880
39 16.95954 38.10107 48.72464
40 18.16482 39.38526 49.01393
41 19.65753 39.65522 41.26681
42 22.88953 38.24876 42.63884
43 20.22844 40.31152 42.63135
44 23.18757 37.63696 48.69777
45 23.40637 46.98266 42.56436
46 23.30492 46.85562 47.50186
47 18.67802 31.60343 49.39261
48 19.08974 33.97059 48.18824
49 19.64843 44.25381 44.83582
50 19.61483 36.58235 43.93127
51 22.78623 41.95128 47.79936
52 28.33482 41.81819 46.77489
53 21.44577 37.15562 40.37073
54 18.26631 39.97675 46.72955
55 19.13408 38.39482 48.17464
56 19.32248 33.61325 47.21720
57 20.89351 35.14826 48.23859
58 19.65785 40.30433 47.44140
59 24.62276 43.31081 40.73070
60 20.99227 51.15512 48.21428
61 19.75040 32.41439 46.43333
62 22.97895 36.41720 41.34554
63 20.74494 36.24732 43.89003
64 15.42535 35.11043 47.80413
65 22.76226 40.81451 43.29891
66 20.24637 45.71448 49.84208
67 24.05644 38.27572 43.24047
68 14.46240 38.63049 49.89981
69 22.14739 44.35675 48.67287
70 20.51287 37.11852 47.65929
71 12.08415 43.00824 43.81002
72 20.52923 40.56529 47.66014
73 22.00610 34.74738 46.31628
74 17.78633 39.31272 42.24558
75 18.39640 42.78462 40.07477
76 15.61842 40.03756 43.88502
77 19.90550 37.29757 49.01647
78 16.98092 45.24377 44.50015
79 16.29129 41.19136 47.21669
80 15.21249 30.76738 47.64929
81 19.53116 42.00846 40.28459
82 20.02867 40.71826 45.86864
83 21.14146 37.98673 44.15859
84 19.54500 40.27188 48.28360
85 18.42598 41.07363 42.96061
86 21.03576 33.89104 49.56412
87 16.72004 36.55947 43.29487
88 21.26478 39.15725 45.77769
89 20.04325 41.54145 43.74594
90 24.72323 43.48947 46.53296
91 19.82423 46.43402 43.90889
92 26.35812 38.34467 42.97377
93 22.92915 43.30351 43.89562
94 16.69641 42.98725 48.08836
95 22.99146 36.61507 49.52636
96 21.46691 43.55045 46.23557
97 19.74177 38.44381 43.06731
98 22.39173 35.35967 49.76410
99 19.23729 40.94000 43.73882
100 19.18005 38.09374 48.71131
> apply(x,MARGIN = 2, mean)
pv uv ip
20.09013 39.87059 45.41095
> apply(x,MARGIN = 2,quantile,probs=c(0.1,0.5,0.9))
pv uv ip
10% 16.05061 34.75776 41.69742
50% 20.03596 40.09494 45.47047
90% 23.50611 44.71334 49.01419
rnorm(n, mean = 0, sd = 1)
n 为产生随机值个数(长度),mean 是平均数, sd 是标准差 。
使用该函数的时候后,一般要赋予它 3个值.
rnorm() 函数会随机正态分布,然后随机抽样 或者取值 n 次
r 这列代表随机,可以替换成dnorm, pnorm, qnorm 作不同计算
r = random = 随机, d= density = 密度, p= probability = 概率 , q =quantile = 分位