码迷,mamicode.com
首页 > 其他好文 > 详细

R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流

时间:2014-05-09 07:45:59      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流

1、创建向量和矩阵

函数c( ), length( ), mode( ), rbind( ), cbind( )

1)创建向量,求向量长度,向量类型。

bubuko.com,布布扣
> x1=c(2,4,6,8,0)
> x2=c(1,3,5,7,9)
> length(x1)
[1] 5
> mode(x1)
[1] "numeric"
bubuko.com,布布扣
bubuko.com,布布扣
> x1
[1] 2 4 6 8 0
> x1[3]
[1] 6
> a1=c(1:100)
> length(a1)
[1] 100
bubuko.com,布布扣

2)创建矩阵,向量合成矩阵。

bubuko.com,布布扣
> 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
bubuko.com,布布扣

2、求平均值,和,连乘,最值,方差,标准差

函数mean( ), sum( ), min( ), max( ), var( ), sd( ), prod( )

bubuko.com,布布扣
> 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
bubuko.com,布布扣

3、寻求帮助

bubuko.com,布布扣
> help(matrix)
> help(mode)
bubuko.com,布布扣

4、一些函数

1)which( )函数

bubuko.com,布布扣
> 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
bubuko.com,布布扣

2)seq( )函数

bubuko.com,布布扣
> 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
bubuko.com,布布扣

3)字母序列letters

bubuko.com,布布扣
> 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 
bubuko.com,布布扣

5、生成矩阵

bubuko.com,布布扣
> 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
bubuko.com,布布扣

6、数据框

bubuko.com,布布扣
> 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
bubuko.com,布布扣

 

R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流,布布扣,bubuko.com

R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/guarder/p/3717780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!