数据类型
包括如下几种类型:
- Array
- Data Frame
- Factor
- Matrix
- Vector
- Complex
- Date/Time
- List
- Operators
Array
格式: array(data=NA,dim=length(data),dimnames=NULL)
代码语言:javascript复制x<-array(1:9)
x
## [1] 1 2 3 4 5 6 7 8 9
x<-array(1:9,c(3,6))
x
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 4 7 1 4 7
## [2,] 2 5 8 2 5 8
## [3,] 3 6 9 3 6 9
x<-1:64
dim(x)<-c(2,4,8)
is.array(x)
## [1] TRUE
x
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 1 3 5 7
## [2,] 2 4 6 8
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 9 11 13 15
## [2,] 10 12 14 16
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 17 19 21 23
## [2,] 18 20 22 24
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 25 27 29 31
## [2,] 26 28 30 32
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 33 35 37 39
## [2,] 34 36 38 40
##
## , , 6
##
## [,1] [,2] [,3] [,4]
## [1,] 41 43 45 47
## [2,] 42 44 46 48
##
## , , 7
##
## [,1] [,2] [,3] [,4]
## [1,] 49 51 53 55
## [2,] 50 52 54 56
##
## , , 8
##
## [,1] [,2] [,3] [,4]
## [1,] 57 59 61 63
## [2,] 58 60 62 64
x[1,,]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1 9 17 25 33 41 49 57
## [2,] 3 11 19 27 35 43 51 59
## [3,] 5 13 21 29 37 45 53 61
## [4,] 7 15 23 31 39 47 55 63
x[1,2,]
## [1] 3 11 19 27 35 43 51 59
x[1,2,1]
## [1] 3
Data Frame
代码语言:javascript复制data(BOD)
x<-BOD
is.matrix(x)
## [1] FALSE
is.data.frame(x)
## [1] TRUE
class(x)
## [1] "data.frame"
x
## Time demand
## 1 1 8.3
## 2 2 10.3
## 3 3 19.0
## 4 4 16.0
## 5 5 15.6
## 6 7 19.8
y<-x[2,]
is.list(y)
## [1] TRUE
is.data.frame(y)
## [1] TRUE
x$Time
## [1] 1 2 3 4 5 7
x$demand
## [1] 8.3 10.3 19.0 16.0 15.6 19.8
attach(x)
demand
## [1] 8.3 10.3 19.0 16.0 15.6 19.8
demand<-demand 10
demand
## [1] 18.3 20.3 29.0 26.0 25.6 29.8
x$demand
## [1] 8.3 10.3 19.0 16.0 15.6 19.8
detach(x)
demand
## [1] 18.3 20.3 29.0 26.0 25.6 29.8
Factor
格式: factor(x = charactor(),levels,labels = levels,exclude = NA,ordered = is.ordered(x))
代码语言:javascript复制v<-c(1,3,5,8,2,1,3,5,3,5)
is.factor(v)
## [1] FALSE
factor(v)
## [1] 1 3 5 8 2 1 3 5 3 5
## Levels: 1 2 3 5 8
x<-factor(v)
x
## [1] 1 3 5 8 2 1 3 5 3 5
## Levels: 1 2 3 5 8
is.factor(x)
## [1] TRUE
x<-factor(v,levels = c(2,1))
levels(x)<-c("two","one")
x
## [1] one <NA> <NA> <NA> two one <NA> <NA> <NA> <NA>
## Levels: two one
Matrix
代码语言:javascript复制A<-matrix(c(3,5,7,1,9,4),nrow = 3,ncol = 2,byrow = TRUE)
A
## [,1] [,2]
## [1,] 3 5
## [2,] 7 1
## [3,] 9 4
rA<-nrow(A)
rA
## [1] 3
cA<-ncol(A)
cA
## [1] 2
B<-t(A)
B
## [,1] [,2] [,3]
## [1,] 3 7 9
## [2,] 5 1 4
C<-A*A
C
## [,1] [,2]
## [1,] 9 25
## [2,] 49 1
## [3,] 81 16
C<-A A
C
## [,1] [,2]
## [1,] 6 10
## [2,] 14 2
## [3,] 18 8
Vector
代码语言:javascript复制v<-c(2,3,5.5,7.1,2.1,3)
v
## [1] 2.0 3.0 5.5 7.1 2.1 3.0
assign("v",c(2,3,5.5,7.1,2.1,3))
c(2,3,5.5,7.1,2.1,3) -> v
v
## [1] 2.0 3.0 5.5 7.1 2.1 3.0
v[3]
## [1] 5.5
1/v
## [1] 0.5000000 0.3333333 0.1818182 0.1408451 0.4761905 0.3333333
2 v
## [1] 4.0 5.0 7.5 9.1 4.1 5.0
v2<--v 1/v 5
v2
## [1] 3.5000000 2.3333333 -0.3181818 -1.9591549 3.3761905 2.3333333
is.vector(v)
## [1] TRUE
is.vector(3,mode = "any")
## [1] TRUE
is.vector(3,mode = "list")
## [1] FALSE
x<-1:10
v
## [1] 2.0 3.0 5.5 7.1 2.1 3.0
x<-rep(2,10)
v
## [1] 2.0 3.0 5.5 7.1 2.1 3.0
v<-seq(1,5,by = 0.5)
v
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
v<-seq(length = 10,from = 1,by =0.5)
v
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
Complex
格式: complex(length.out=0,real=numeric(),imaginary=numeric(),modulus=1,argument=0)
函数
- as.complex()
- is.complex()
- Re()
- Im()
- Mod()
- Arg()
- Conj()
- length.out: Desired length of the output vector, inputs being recycled as needed
library(graphics)
z<-complex(real = stats::rnorm(100),imaginary = stats::rnorm(100))
z
## [1] 0.5200295-2.2646912i -0.5322567 0.0779762i -2.3800465 0.7640518i
## [4] 0.7011146-1.5866943i -1.5544585 0.9682606i 2.5980975-0.5449572i
## [7] -1.0006683-0.0431467i 1.3431015-1.8346469i -0.4170697-0.1060395i
## [10] 1.0495727 0.2481264i -1.1598388-0.3027608i -1.1415871-0.9839143i
## [13] 0.9634596-0.3406711i -0.6469331 0.5942862i 0.3491618 1.1139532i
## [16] -0.2643147 0.0519278i 0.6534257-1.3681752i -2.0621975 1.7368652i
## [19] -0.4978312 2.4327759i 0.2072797-1.4709151i -1.1051954 0.3610744i
## [22] -0.5659170-0.6280635i 0.8336062-1.2781031i -0.0378319 0.7158486i
## [25] -1.6976023 0.1562052i 0.9617835-0.4216835i -1.5994119 0.0569988i
## [28] 0.0514633-0.2842285i -0.6288963 0.2978067i 1.7278770-0.7497994i
## [31] 1.0417099 0.0707912i 0.5190370 0.7463731i 0.4725722 0.8511658i
## [34] 0.0945794-0.8224629i -0.7348969 0.2633703i -0.3660376 0.7127934i
## [37] 1.3866084 0.5275001i 1.5448222 0.8082621i 1.2042018-0.1581438i
## [40] -1.4237678 0.1370675i -0.0881483 2.8495642i -0.0400514-0.2569562i
## [43] -0.6825624-1.0050652i 2.5456405 1.6611583i 0.0205514-0.6191978i
## [46] 0.5180435 0.3025533i 0.3526055 0.6939888i -0.1410374-1.6014629i
## [49] 0.8880902 0.0202024i -0.4442246 0.2276906i 0.0481735-0.9808916i
## [52] 0.5404179 0.6190054i -1.4538082 2.3068149i 0.4927693-0.0660740i
## [55] 1.3784518 0.7746101i 0.4527109-1.0455589i 0.2950279-0.8670672i
## [58] 0.6241538-1.3237922i 0.7379039-0.3067607i 1.3496676-1.1139908i
## [61] -0.3770614 0.1915712i -0.7320517-1.0273366i 0.7041309 0.4240790i
## [64] -0.8238194-0.5443358i -0.1569915 0.4223923i -0.0821708 0.4785644i
## [67] 1.2273175 0.1608278i -1.6662780 0.2332501i 0.3141801 0.6644287i
## [70] -0.2627204 0.1527057i 0.6326420 1.6541494i -3.0225302-0.0828999i
## [73] -0.5267586-0.5149930i -0.1209515 0.9474985i -0.4087781 0.5585448i
## [76] 1.2355654-0.3827317i 0.6464085 1.1398532i 1.3652592 0.6154433i
## [79] -1.9035074-0.1179588i 0.8216094 3.0352598i 0.5479892 0.0764115i
## [82] 0.6781626 0.1278338i 0.3997126-0.6050511i 0.0183661-1.2055350i
## [85] 0.1103128 0.1660670i 0.6035714 0.7507500i 0.1462197 0.2927559i
## [88] 0.6771124 2.4199871i -0.4632352-0.6433767i -3.1092950-1.6220725i
## [91] -2.1641098 0.2462180i 0.0804014 0.8360996i 0.6468068 1.2322475i
## [94] -0.9611167 0.0499096i 1.2155416-0.1811604i 1.1560557-0.5548846i
## [97] 0.2000294-0.9172184i -0.1139657 0.9069213i 1.1837461 1.2375861i
## [100] 0.5024251 0.5594804i
z1<-1:2 1i*(8:9)
z1
## [1] 1 8i 2 9i
zz<-(rep(1:4,len = 9) 1i*(9:1))/10
zz
## [1] 0.1 0.9i 0.2 0.8i 0.3 0.7i 0.4 0.6i 0.1 0.5i 0.2 0.4i 0.3 0.3i 0.4 0.2i
## [9] 0.1 0.1i
zz.shift<-complex(modulus = Mod(zz),argument = Arg(zz) pi)
plot(zz,xlim = c(-1,1),ylab = c(-1,1),col = "red",asp = 1,
main = expression(paste("Rotation by","",pi == 180^o)))
abline(h = 0,v = 0,col = "blue",lty = 3)
points(zz.shift,col = "orange")
Date/Time
代码语言:javascript复制date()
## [1] "Sat Jul 23 18:48:06 2016"
Sys.Date()
## [1] "2016-07-23"
Sys.time()
## [1] "2016-07-23 18:48:06 CST"
class(date())
## [1] "character"
class(Sys.Date())
## [1] "Date"
class(Sys.time())
## [1] "POSIXct" "POSIXt"
x<-"19:18:05"
y<-strptime(x,"%H:%M:%S")
y
## [1] "2016-07-23 19:18:05 CST"
class(y)
## [1] "POSIXlt" "POSIXt"
y$sec
## [1] 5
其他格式的时间类型:
List
代码语言:javascript复制x<-list(batch = 3,label = "Lung Cancer Patients",subtype = c("A","B","C"))
x
## $batch
## [1] 3
##
## $label
## [1] "Lung Cancer Patients"
##
## $subtype
## [1] "A" "B" "C"
is.list(x)
## [1] TRUE
x[[1]]
## [1] 3
x[[2]]
## [1] "Lung Cancer Patients"
x[[3]]
## [1] "A" "B" "C"
x[[3]][2]
## [1] "B"
x$subtype
## [1] "A" "B" "C"
x[["subtype"]]
## [1] "A" "B" "C"
length(x)
## [1] 3
y<-list(operator = "Mary",location = "New York")
y
## $operator
## [1] "Mary"
##
## $location
## [1] "New York"
z<-list(cost = 1000.24,urgent = "yes")
z
## $cost
## [1] 1000.24
##
## $urgent
## [1] "yes"
final_list<-c(x,y,z)
final_list
## $batch
## [1] 3
##
## $label
## [1] "Lung Cancer Patients"
##
## $subtype
## [1] "A" "B" "C"
##
## $operator
## [1] "Mary"
##
## $location
## [1] "New York"
##
## $cost
## [1] 1000.24
##
## $urgent
## [1] "yes"
y<-as.data.frame(x)
y
## batch label subtype
## 1 3 Lung Cancer Patients A
## 2 3 Lung Cancer Patients B
## 3 3 Lung Cancer Patients C
y<-as.matrix(x)
y
## [,1]
## batch 3
## label "Lung Cancer Patients"
## subtype Character,3
Operators
操作数 - / ^ %% %/% < > = <= >= != ! | &
代码语言:javascript复制' ' <- function(x,y) x*y
3 5
## 15
rm(' ')
3 5
## 8