1.变量赋值
> x=c(1,3,4,6,3,2)
> x
[1] 1 3 4 6 3 2
2.查看变量内容类型
> x=c(1,3,4,6,3,2)
> mode(x)
[1] "numeric"
> y=c("a","d","xra")
> mode(y)
[1] "character"
3.帮助命令
> help(mode)
starting httpd help server ... done
4.变量长度
> x1=c(2,4,6,8,0)
> x2=c(1,3,5,7,9)
> length(x1)
[1] 5
> x1
[1] 2 4 6 8 0
> x1[3]
[1] 6
> a1=c(1:100)
> length(a1)
[1] 100
5.把向量按行和列组成矩阵
> x1=c(2,4,6,8,0)
> x2=c(1,3,5,7,9)
> rbind(x1,x2)
[,1] [,2] [,3] [,4] [,5]
x1 2 4 6 8 0
x2 1 3 5 7 9
> m1=rbind(x1,x2)
> m1
[,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
6.向量的计算
6.1 命令快速取值
> x=c(1:100)
> mean(x)
[1] 50.5
> sum(x)
[1] 5050
> max(x)
[1] 100
> min(x)
[1] 1
> var(x)
[1] 841.6667
> prod(x)
[1] 9.332622e+157
> sd(x)
[1] 29.01149
mean:求平均值
sum:求和
max、min:求最大值、最小值
var:求方差(离散程度)
prod:连乘,从123*…*100
sd:求标准差
6.2 计算
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
> 1:10-1
[1] 0 1 2 3 4 5 6 7 8 9
> 1:10*2
[1] 2 4 6 8 10 12 14 16 18 20
> 2:60*2+1
[1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
[19] 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
[37] 77 79 81 83 85 87 89 91 93 95 97 99 101