您的当前位置:首页正文

R语言菜鸟练习笔记1

2020-01-14 来源:客趣旅游网
 R语言笔记

1.制作一个1维10元素数组, 分别取其5,3~5,(1,3,5,7)元素

> a<-c(1:10) > c(5) [1] 5 > c(3:5) [1] 3 4 5 > c(1,3,5,7) [1] 1 3 5 7

2.制作一个2维5x6元素数组 分别取其3行、4列元素 取其(1,3)行元素

取其(1,3)行、(2,5)列元素

> x<-matrix(1:30,5,6) > x

[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 6 11 16 21 26 [2,] 2 7 12 17 22 27 [3,] 3 8 13 18 23 28 [4,] 4 9 14 19 24 29 [5,] 5 10 15 20 25 30 > x[3,]

[1] 3 8 13 18 23 28 > x[4,]

[1] 4 9 14 19 24 29 > x[seq(1,3,2),]

[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 6 11 16 21 26 [2,] 3 8 13 18 23 28 > x[seq(1,3,2),seq(2,5,3)] [,1] [,2] [1,] 6 21 [2,] 8 23

3.输入一个10元素的向量 求其平均值、最大值、最小值、和 对其进行排序输出 大->小 小->大

求其中位数(15元素的中位数是?)

> y<-c(1:10)> y [1] 1 2 3 4 5 6 7 8 9 10 > mean(y) [1] 5.5 > max(y) [1] 10 > min(y) [1] 1 > sum(y) [1] 55

> sort(y,decreasing=FALSE) [1] 1 2 3 4 5 6 7 8 9 10 > sort(y,decreasing=TRUE) [1] 10 9 8 7 6 5 4 3 2 1

4.使用scan方式读入5x6矩阵 取其(1,3,4)列

> x<-matrix(data<-scan(),5,6) 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 10: 10 11: 11 12: 12 13: 13

14: 14 15: 15 16: 16 17: 17 18: 18 19: 19 20: 20 21: 21 22: 22 23: 23 24: 24 25: 25 26: 26 27: 27 28: 28 29: 293 0: 30 31:

Read 30 items >x

[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 6 11 16 21 26 [2,] 2 7 12 17 22 27 [3,] 3 8 13 18 23 28 [4,] 4 9 14 19 24 29 [5,] 5 10 15 20 25 30 > x[,c(1,3,4)] [,1] [,2] [,3] [1,] 1 11 16 [2,] 2 12 17 [3,] 3 13 18 [4,] 4 14 19 [5,] 5 15 20

5.读入一个csv文件,输出,并求其平均数

>a<-read.csv(\"C:/Users/win7/Desktop/r.csv\> a V1 V2 1 1 6 2 2 7 3 3 8 4 4 9 5 5 10

b<-c(1:10) > mean(b) [1] 5.5

6.生成100个[-100,100]均匀分布随机数 求其平均值、最大值、最小值

> h<-runif(100,min=-100,max=100) > h

[1] -36.0731443 97.2143370 51.2432668 -28.2452125 -42.7659259 15.8886953

[7] 68.8943439 56.9598435 42.3795265 -26.0053386 81.1291242 54.0708309

[13] 44.7517761 -35.5996959 -99.5348976 -33.9110530 -91.8305716 43.4921531

[19] -22.9264680 31.8134199 -20.9213133 -87.1943854 -45.4408710 43.8530930

[25] 3.4081669 47.4633112 -92.1261621 -2.4796658 -0.3442111 95.9171894

[31] -4.9284740 96.0699261 -52.7216334 4.2518115 -45.9512866 34.2404426

[37] -55.5040050 -4.4156488 -47.3706595 -82.5223469 67.5399391 38.0685795

[43] -0.5257283 -90.9271230 3.3324568 19.8671467 60.5416961 91.0444072

[49] -29.9325735 -20.0840082 -18.5377186 -29.4716766 90.5238681 84.0294338

[55] -16.7638883 13.2257728 86.1513265 82.6287232 -10.8205724 -16.4097079

[61] 72.8469722 -57.6760801 37.2857811 -52.9833146 -63.8799985 79.3569705

[67] -37.8343046 -83.1496577 61.1786120 71.7845275 -88.0638801 59.2558075

[73] -14.9232914 51.1255557 59.4956463 81.8489698 -16.1780055 91.3360897

[79] 2.0163156 91.0086562 -11.2780071 -59.8128892 -94.9187696 -99.8394703

[85] 23.4650509 -73.1315227 92.0865325 97.6030794 -67.1836372 19.0384018

[91] 97.2064479 -69.9629383 57.3518248 -76.1122700 -9.4561099 -88.2496682

[97] 98.9648074 -38.1344582 20.0970254 75.3511773 > mean(h) [1] 5.946446 > max(h) [1] 98.96481 > min(h) [1] -99.83947

7.生成100个[-100,100]均匀分布随机数 保存至csv文件中

> a<-runif(100,-100,100) > save(a,file='d:/r.csv')

8.生成0到2之间的50个随机数,分别命名为x,y

绘制其散点图

将主标题命名为“散点图”, 横轴命名为”横坐标”, 纵轴命名为“纵坐标”

> x<-runif(50,0,2) > y<-runif(50,0,2)

> plot(x,y,main=\"散点图\横坐标\纵坐标\")

因篇幅问题不能全部显示,请点此查看更多更全内容