R语言高级数据管理

一,数学函数

绝对值函数abs(x)

sqrt(x) 开平方根

不小于某个数的最小整数ceiling(x)

不大于某个数的最大整数floor(x)

四舍五入round(x)

sin(x)

cos(x)

log(x)

二,统计函数

求平均值

> x<-c(2,3,4,5,6,7,8,9,10)
> mean(x)

求和

> a<-c(2,3,4,5,6)
> sum(a)
[1] 20

 求标准差

> sd(a)
[1] 1.581139

 方差

> var(a)
[1] 2.5

 求最大值

> max(a)
[1] 6

 求最小值

> min(a)
[1] 2

 标准化

> scale(a)
           [,1]
[1,] -1.2649111
[2,] -0.6324555
[3,]  0.0000000
[4,]  0.6324555
[5,]  1.2649111
attr(,"scaled:center")
[1] 4
attr(,"scaled:scale")
[1] 1.581139

 

三,概率函数

 正态函数:

> x<-pretty(c(-3,3),30)
> x
 [1] -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2  0.0  0.2  0.4  0.6  0.8  1.0  1.2  1.4  1.6  1.8  2.0  2.2  2.4  2.6  2.8  3.0
> y<-dnorm(x)
> plot(x,y)

 其他正态函数

> rnorm(50,mean=20,sd=8)
 [1] 15.01154288 38.11096393 18.65391796 14.38228071 11.60227115  8.36768099  6.78851143 -0.06757863 15.51745301 22.42118256 23.37286503 20.61214282  5.59927021
[14] 23.69614839 17.71606857 29.69719738 27.25146139 27.97797925 11.75969538 30.84565356  3.13559481 -2.82207130 13.43633698 29.48021201 29.33732577 26.96600429
[27] 24.92109400 18.97664020 14.56857325 11.06500051 10.28361728 25.43226224  3.36034989 24.98450651 28.53060037 23.46619776 29.30594271 14.48793170  8.82698624
[40] 31.50511932 11.04945488 18.97749006 22.50621256 21.05814273 33.22670323 20.52845380 11.46026542 18.06046786 19.08941141  8.54350999
> runif(5)
[1] 0.0575840 0.8907667 0.3178646 0.1195145 0.6993192
> 
> runif(5)
[1] 0.1634405 0.3271568 0.7107670 0.8365320 0.909055
//保持生成的随机数不变
> set.seed(12)
> runif(5)
[1] 0.06936092 0.81777520 0.94262173 0.26938188 0.16934812
> set.seed(12)
> runif(5)
[1] 0.06936092 0.81777520 0.94262173 0.26938188 0.16934812

四,字符串处理函数

获取长度

> x<-"abced"
> nchar(x)
[1] 5

 截取字符串

> substr(x,3,5)
[1] "ced"

获取某个字符串的索引位置

> grep("a",x)
[1] 1

替换字符串

> sub("a","A",x)
[1] "Abced

分割字符串

> strsplit(x,"c")
[[1]]
[1] "ab" "ed"

拼接字符串

> paste("today",x)
[1] "today abced"

转换大小写

> toupper(x)
[1] "ABCED"
> tolower(x)
[1] "abced"

五,其他类型函数

获取长度

> length(x)
[1] 1

生成等差数列

> seq(1,10,2)
[1] 1 3 5 7 9

重复某个数

> rep(1:3,2)
[1] 1 2 3 1 2 3

打印日志

> cat("Hello","Tom")
Hello Tom

六,将函数应用于矩阵和数列

> b<-matrix(runif(12),nrow=3)
> b
           [,1]        [,2]      [,3]      [,4]
[1,] 0.03389562 0.022877743 0.8138806 0.2649184
[2,] 0.17878500 0.008324827 0.3762485 0.4393343
[3,] 0.64166537 0.392697197 0.3808122 0.4576072
> log(b)
           [,1]       [,2]       [,3]       [,4]
[1,] -3.3844694 -3.7775907 -0.2059417 -1.3283335
[2,] -1.7215713 -4.7885131 -0.9775056 -0.8224946
[3,] -0.4436883 -0.9347165 -0.9654490 -0.7817442
> mean(b)
[1] 0.3342539

//对行求平均值
> apply(b, 1,mean)
[1] 0.2838931 0.2506732 0.4681955
//对列求平均值
> apply(b, 2,mean)
[1] 0.2847820 0.1412999 0.5236471 0.3872866

七,重复和循环

for循环

> for (i in 1:5) {
+   print("Hello R")  
+ }
[1] "Hello R"
[1] "Hello R"
[1] "Hello R"
[1] "Hello R"
[1] "Hello R"

while循环 

> x<-5
> while (x>0) {
+     print("Hello R");x<-x-1
+ }
[1] "Hello R"
[1] "Hello R"
[1] "Hello R"
[1] "Hello R"
[1] "Hello R"

八,条件执行

if-else

> if(x!=1)print("male") else print("female")
[1] "female

ifelse

> ifelse(x>1,print("male"),print("female"))
[1] "female"
[1] "female"

switch

> a<-c("abb","cdd")

> for (i in a) print(switch(i,abb="11",cdd="2",other="3"))
[1] "11"
[1] "2"

九,转置

将数据集的行列转置

> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
> cars<-mtcars[1:5,1:4]
> cars
                   mpg cyl disp  hp
Mazda RX4         21.0   6  160 110
Mazda RX4 Wag     21.0   6  160 110
Datsun 710        22.8   4  108  93
Hornet 4 Drive    21.4   6  258 110
Hornet Sportabout 18.7   8  360 175
> t(cars)
     Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout
mpg         21            21       22.8           21.4              18.7
cyl          6             6        4.0            6.0               8.0
disp       160           160      108.0          258.0             360.0
hp         110           110       93.0          110.0             175.0

上一篇:Django后台项目开发实战七-第七阶段


下一篇:配置有效的防爬虫技术保护网站