1、查看当前工作空间
> getwd() [1] "C:/Users/P0079482.HHDOMAIN/Documents" >
2、查看内存中有哪些对象
> ls() [1] "a" "a1" "b" "bank" "bank_full" "dat" [7] "m1" "tab" "w" "x" "x1" "x2" >
3、把指定对象从内存中删除
> rm(\'a\') > ls() [1] "a1" "b" "bank" "bank_full" "dat" "m1" [7] "tab" "w" "x" "x1" "x2" >
4、查看函数帮助
> help(matrix) >
5、创建向量和矩阵
> x1=c(2,4,6,8,0) > x2=c(1,3,5,7,9)
查看向量的长度 > length(x1) [1] 5
查看向量的类型
> mode(x1) [1] "numeric" >
6、按行将向量排列成矩阵
> rbind(x1,x2) [,1] [,2] [,3] [,4] [,5] x1 2 4 6 8 0 x2 1 3 5 7 9
7、按列将向量排成矩阵
> cbind(x1,x2) x1 x2 [1,] 2 1 [2,] 4 3 [3,] 6 5 [4,] 8 7 [5,] 0 9
8、求向量的均值
> x=c(1:100) > mean(x) [1] 50.5 >
9、求向量的和
> sum(x) [1] 5050 >
10、
> max(x) 求最大值 [1] 100 > min(x) 最小值 [1] 1 > var(x) 方差 [1] 841.6667 > prod(x) 连乘 [1] 9.332622e+157 > sd(x) 标准差 [1] 29.01149
11、生成矩阵
> 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 1 2 根据行生成矩阵
12、矩阵的转置
> a=matrix(1:12,nrow=3,ncol=4) > a [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > t(a) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 >
13、矩阵的加减
> a=b=matrix(1:12,nrow=3,ncol=4) > a+b [,1] [,2] [,3] [,4] [1,] 2 8 14 20 [2,] 4 10 16 22 [3,] 6 12 18 24 > a-b [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 >
14、矩阵乘法
> a=matrix(1:12,nrow=3,ncol=4) > b=matrix(1:12,nrow=4,ncol=3) > a%*%b [,1] [,2] [,3] [1,] 70 158 246 [2,] 80 184 288 [3,] 90 210 330 >
15、矩阵求对角元素
> a=matrix(1:16,nrow=4,ncol=4) > a [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > diag(a) [1] 1 6 11 16 > diag(diag(a)) [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 6 0 0 [3,] 0 0 11 0 [4,] 0 0 0 16 > diag(4) [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 1 0 0 [3,] 0 0 1 0 [4,] 0 0 0 1 >
16、生成随机矩阵
> a=matrix(rnorm(16),4,4) > a [,1] [,2] [,3] [,4] [1,] 0.2353978 -1.168817665 -0.03914636 -0.4350940 [2,] 0.5550182 -0.001076645 -1.92283070 1.1007430 [3,] 0.2582714 -0.846160178 0.94940298 -0.6125362 [4,] -2.1307575 -2.478207744 -0.44198013 -0.2581712 >
17、矩阵求逆
> solve(a) [,1] [,2] [,3] [,4] [1,] 0.2524842 0.3182516 0.5172485 -0.2958290 [2,] 0.4128992 -0.5045100 -1.1071800 -0.2200014 [3,] -1.7116774 0.4246262 1.9141867 0.1535275 [4,] -3.1169442 1.4892720 3.0819024 0.4171374 >
18、基本的数据结构,数据框
> 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(\'weight\'=x1,\'cost\'=x2)) weight cost 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 >
19、读取文本文件、
> (x=data.frame(\'weight\'=x1,\'cost\'=x2)) weight cost 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=read.table("abc.txt")) V1 V2 1 175 67 2 183 75 3 165 56 4 145 45 5 178 67 6 187 90 7 156 43 8 176 58 9 173 60 10 170 56
20、读取excell文件
> w<-read.table("test.prn",header = T) > w X.. X...1 1 A 2 2 B 3 3 C 5 4 D 5 >
21、用readxl包读excell
> library(readxl) > dat<-read_excel("test.xlsx") > dat # A tibble: 4 x 2 `商品` `价格` <chr> <dbl> 1 A 2 2 B 3 3 C 5 4 D 5 >
请发表评论