标签:style blog class code java color
R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流
1、创建向量和矩阵
函数c( ), length( ), mode( ), rbind( ), cbind( )
1)创建向量,求向量长度,向量类型。
> x1=c(2,4,6,8,0) > x2=c(1,3,5,7,9) > length(x1) [1] 5 > mode(x1) [1] "numeric"
> x1 [1] 2 4 6 8 0 > x1[3] [1] 6 > a1=c(1:100) > length(a1) [1] 100
2)创建矩阵,向量合成矩阵。
> rbind(x1,x2) [,1] [,2] [,3] [,4] [,5] x1 2 4 6 8 0 x2 1 3 5 7 9 > cbind(x1,x2) x1 x2 [1,] 2 1 [2,] 4 3 [3,] 6 5 [4,] 8 7 [5,] 0 9 > m1=rbind(x1,x2) > m1 [,1] [,2] [,3] [,4] [,5] x1 2 4 6 8 0 x2 1 3 5 7 9
2、求平均值,和,连乘,最值,方差,标准差
函数mean( ), sum( ), min( ), max( ), var( ), sd( ), prod( )
> x=c(1:100) > mean(x) [1] 50.5 > sum(x) [1] 5050 > max(x) [1] 100 > min(x) [1] 1 > var(X) 错误于is.data.frame(x) : 找不到对象‘X‘ > var(x) [1] 841.6667 > prod(x) [1] 9.332622e+157 > sd(x) [1] 29.01149
3、寻求帮助
> help(matrix)
> help(mode)
4、一些函数
1)which( )函数
> a=c(2,3,4,2,5,1,6,3,2,5,8,5,7,3) > which.max(a) [1] 11 > which.min(a) [1] 6 > a[which.max(a)] [1] 8 > which(a==2) [1] 1 4 9 > a[which(a==2)] [1] 2 2 2 > which(a>5) [1] 7 11 13 > a[which(a>5)] [1] 6 8 7
2)seq( )函数
> seq(5,20) [1] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > seq(5,121,by=2) [1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 [23] 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 [45] 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 > seq(5,121,by=2,length=10) 错误于seq.default(5, 121, by = 2, length = 10) : 太多参数 > seq(5,121,length=10) [1] 5.00000 17.88889 30.77778 43.66667 56.55556 69.44444 82.33333 95.22222 [9] 108.11111 121.00000
3)字母序列letters
> letters[1:30] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" [23] "w" "x" "y" "z" NA NA NA NA
5、生成矩阵
> a1=c(1:12) > matrix(a1,nrow=3,ncol=4) [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > matrix(a1,nrow=4,ncol=3) [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 > matrix(a1,nrow=4,ncol=3,byrow=T) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12
6、数据框
> x1=c(10,13,45,26,23,12,24,78,23,43,31,56) > x2=c(20,65,32,32,27,87,60,13,42,51,77,35) > x=data.frame(x1,x2) > x x1 x2 1 10 20 2 13 65 3 45 32 4 26 32 5 23 27 6 12 87 7 24 60 8 78 13 9 23 42 10 43 51 11 31 77 12 56 35 > x=data.frame(‘重量‘=x1,‘运费‘=x2) > x 重量 运费 1 10 20 2 13 65 3 45 32 4 26 32 5 23 27 6 12 87 7 24 60 8 78 13 9 23 42 10 43 51 11 31 77 12 56 35
R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流,布布扣,bubuko.com
R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流
标签:style blog class code java color
原文地址:http://www.cnblogs.com/guarder/p/3717780.html