标签:
#清除所有变量 rm(list=ls()) #清除单个变量 rm(data) #清除多个变量 rm(list=c(‘data1‘,‘data2‘))
> str(a) ‘data.frame‘: 13 obs. of 3 variables: $ class: Factor w/ 7 levels "浜岀彮","涓夌彮",..: 7 6 5 4 3 1 1 1 1 2 ... $ name : Factor w/ 10 levels "","122","闄堜附鐏\xb5,115",..: 1 2 1 1 2 8 7 6 5 4 ... $ score: int NA NA NA NA NA 119 96 NA NA NA ...
> attributes(a) $names [1] "class" "name" "score" $class [1] "data.frame" $row.names [1] 1 2 3 4 5 6 7 8 9 10 11 12 13
> vector(‘numeric‘,5)#numeric(5) [1] 0 0 0 0 0 > vector(‘logical‘,5)#logical(5) [1] FALSE FALSE FALSE FALSE FALSE > vector(‘character‘,5)#character(5) [1] "" "" "" "" "" > vector(‘list‘,2) [[1]] NULL [[2]] NULL
> letters[4] [1] "d" > letters[1:4] [1] "a" "b" "c" "d" > letters[] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" [13] "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" [25] "y" "z"
#runif(n,min,max),默认产生0-1之间的随机数 > runif(3) [1] 0.3308360 0.5573855 0.9720616 > runif(3,min=1,max=9) [1] 8.946071 4.915268 5.763918 #rnom可指定参数mean:平均数,sd:标准差,默认mean=0,sd=1 > rnorm(5) [1] 0.55080064 -0.24792330 -0.09480838 0.65215600 [5] -0.96422061 > rnorm(5,mean=2,sd=0.3) [1] 2.373522 2.176204 1.948216 2.371155 2.070872
#seq.int创建一个序列,与冒号有所不同,指定步长时也不一样 > 2:8 [1] 2 3 4 5 6 7 8 > seq.int(2:8) [1] 1 2 3 4 5 6 7 > seq.int(4,10,3)#指定步长3 [1] 4 7 10 #seq_len函数创建一个从1到它的输入值的序列,输入为0时创建一个空值 > seq_len(0) integer(0) > attributes(seq_len(0)) NULL #seq_along创建一个从1开始、长度为其输入值的序列 > seq_along(letters[1:10]) [1] 1 2 3 4 5 6 7 8 9 10
#整体重复三遍 > rep(1:5,3) [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 #每个元素重复三遍 > rep(1:5,each=3) [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 #每个元素重复个数:1:5 > rep(1:5,times=1:5) [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 #重复矢量直到达到15的长度 > rep(1:5,length.out=15) [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
#array创建数组,另个参数:值,维度dim > array(1:12,dim=c(3,4)) [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 #matrix创建矩阵,需要指定行数或者列数,默认输入的值按照列填充,或者指定参数byrow=TRUE按行填充 > matrix(1:12,nrow=3) [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > matrix(1:12,nrow=3,byrow=TRUE) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12
> length(1:5) [1] 5 #缺失值NA也被计入长度 > length(c(TRUE,FALSE,NA)) [1] 3 #返回字符串的数目 > length(c(‘A‘,‘B‘,‘C‘)) [1] 3 #若想返回每个字符串中字符的长度用nchar > nchar(c(‘Axc‘,‘Bcdsfs‘,‘Cdsss‘)) [1] 3 6 5
若用于矩阵和数组则返回所有维度的乘积
可以在创建数据时指定名称,也可以在事后命名。
#kiwi fruit用引号括起来了,因为其名称无效:有空格 #可以忽略其他元素命名 > c(apple=1,"kiwi fruit"=2,3) apple kiwi fruit 1 2 3 > x <- 1:3 > names(x) <- c("apple","kiwi fruit",‘‘) > x apple kiwi fruit 1 2 3 #names函数返回名称,若没有一个元素有名字则返回NULL > names(x) [1] "apple" "kiwi fruit" "" > names(1:4) NULL #创建数组时命名,矩阵同样 > x <- array(1:6,dim=c(2,3),dimnames=list(c("one","tow"),c("ein","zwei","drei"))) > x ein zwei drei one 1 3 5 tow 2 4 6 #rownames、colnames和dimnames函数 > rownames(x) [1] "one" "tow" > colnames(x) [1] "ein" "zwei" "drei" > dimnames(x) [[1]] [1] "one" "tow" [[2]] [1] "ein" "zwei" "drei"
dim函数对于数组和矩阵返回所有维度。nrow和ncol函数分别返回行和列。norm、ncol和dim用于向量时返回NULL。NROW和NCOL函数把向量看做列向量
c函数拼接矩阵把他们转换成向量,cbind和rbind分别按col列和row行合并矩阵
> x <- matrix(1:12,nrow=3);x [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > y <- matrix(13:24,nrow=3);y [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24 > c(x,y) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [17] 17 18 19 20 21 22 23 24 > cbind(x,y) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 4 7 10 13 16 19 22 [2,] 2 5 8 11 14 17 20 23 [3,] 3 6 9 12 15 18 21 24 > rbind(x,y) [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 [4,] 13 16 19 22 [5,] 14 17 20 23 [6,] 15 18 21 24
除法/,转置t函数,点乘%*%和外乘%o%。solve矩阵求反
向量内乘 > 1:3%*%2:4 [,1] [1,] 20 #向量外乘 > 1:3%o%2:4 [,1] [,2] [,3] [1,] 2 3 4 [2,] 4 6 8 [3,] 6 9 12 > x<-matrix(1:6,nrow=2);x [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > y<-matrix(1:6,nrow=3);y [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 #矩阵相乘 > x%*%y [,1] [,2] [1,] 22 49 [2,] 28 64 > x<-matrix(runif(9),nrow=3);x [,1] [,2] [,3] [1,] 0.6136448 0.05304645 0.7378249 [2,] 0.1922476 0.18177634 0.9545918 [3,] 0.9050422 0.65400112 0.2954112 #矩阵求逆 > solve(x) [,1] [,2] [,3] [1,] 1.6984882 -1.389698 0.2484943 [2,] -2.4026083 1.448092 1.3214365 [3,] 0.1154494 1.051693 -0.3016769 #矩阵转置 > t(x) [,1] [,2] [,3] [1,] 0.98387863 0.6292973 0.03753178 [2,] 0.09061386 0.5971163 0.10537436 [3,] 0.80727162 0.5403768 0.55767250
标签:
原文地址:http://www.cnblogs.com/zhangduo/p/4537168.html