-
#向量 #生成一个指定长度和类型的空向量 X <- vector("character",length = 10) #包含左边数字的向量,偏移量为1,数据类型以左边数字为准 X1 <- 5.5:-2 #指定向量 X2 <- c(1,3,5,7,1.2) #自动强制转换数据类型(优先级:字符>复数>数值>整数>逻辑) X3 <- c(TRUE,12L,12,12+12i,"ww") #指定类型强制转换,引入缺失类型NA(强转不了的) #字符 as.character(X3) #复数 as.complex(X3) #数值 as.numeric(X3) #整数 as.integer(X3) #逻辑 as.logical(X3) #给向量加名称 class(X2) names(X2) <- c("a1","a2","a3","a4","a5") X2查看全部
-
数据框(data frame)
一、存储表格数据(tabular data)
二、视为各元素长度相同的列表
(1)每个元素代表一列数据
(2)每个元素的长度代表行数
(3)元素类型可以不同
创建数据框:df <- data.frame(id = c(1,2,3,4), name = c("a","b","c","d"), gender = c(TRUE,TRUE,FALSE,FALSE))
查看数据框行列数:nrow(df), ncol(df)
某些特殊的数据框(每个元素的数据类型一样)可以转换为矩阵:
data.matrix(df2) (df2为一元素数据类型一样的数据框)
查看全部 -
缺失值(missing value)
一、NA/NaN:NaN属于NA,NA不属于NaN(NaN一般表示的是数字的缺失值,NA广泛一点儿)
二、NA有类型属性:integer NA, character NA等
三、is.na()/is.nan() (判断是否有缺失值)
查看全部 -
列表
l <- list("a", 2, 10L, 3+4i, TRUE)
列表命名
l2 <- list(a=1, b=2, c=3)
列表中每个元素中的元素个数大于1
l3 <- list(c(1,2,3), c(4,5,6,7))
矩阵行列命名
dimnames(x) <- list(c("a","b"), c("c","d","e"))
(x是一个矩阵,a.b是行的名字,d,e是列的名字)
查看全部 -
矩阵的创建(行列): x <- matrix(nrow = 3, ncol = 2)
x <-matrix(1:6, nrow = 3, ncol = 2)
矩阵的维度:dim(x)
属性:attributes(x)
矩阵按行(行增加)拼接:rbind(x,y);按列拼接:cbind(x,y).
创建数组:x <- array(1:24,dim=c(4,6))
x <- array(1:24,dim=c(2,3,4))
查看全部 -
> o <- array(1:100,dim = c(4,5,4))> o, , 1 [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 , , 2 [,1] [,2] [,3] [,4] [,5] [1,] 21 25 29 33 37 [2,] 22 26 30 34 38 [3,] 23 27 31 35 39 [4,] 24 28 32 36 40 , , 3 [,1] [,2] [,3] [,4] [,5] [1,] 41 45 49 53 57 [2,] 42 46 50 54 58 [3,] 43 47 51 55 59 [4,] 44 48 52 56 60 , , 4 [,1] [,2] [,3] [,4] [,5] [1,] 61 65 69 73 77 [2,] 62 66 70 74 78 [3,] 63 67 71 75 79 [4,] 64 68 72 76 80
> 查看全部 -
> o <- array(1:100,dim = c(20,5))> o [,1] [,2] [,3] [,4] [,5] [1,] 1 21 41 61 81 [2,] 2 22 42 62 82 [3,] 3 23 43 63 83 [4,] 4 24 44 64 84 [5,] 5 25 45 65 85 [6,] 6 26 46 66 86 [7,] 7 27 47 67 87 [8,] 8 28 48 68 88 [9,] 9 29 49 69 89 [10,] 10 30 50 70 90 [11,] 11 31 51 71 91 [12,] 12 32 52 72 92 [13,] 13 33 53 73 93 [14,] 14 34 54 74 94 [15,] 15 35 55 75 95 [16,] 16 36 56 76 96 [17,] 17 37 57 77 97 [18,] 18 38 58 78 98 [19,] 19 39 59 79 99 [20,] 20 40 60 80 100
> 查看全部 -
> cbind(p,q) [,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> cbind9q,pError: unexpected ',' in "cbind9q,"> cbind(q,p) [,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 <- c(1:20,dim(4,5))Error in dim(4, 5) : 2 arguments passed to 'dim' which requires 1> x <- matrix(1:20,dim(4,5))Error in dim(4, 5) : 2 arguments passed to 'dim' which requires 1> x <- array(1:20,dim(4,5))Error in dim(4, 5) : 2 arguments passed to 'dim' which requires 1> h <- array(1:16,dim(2,8))Error in dim(2, 8) : 2 arguments passed to 'dim' which requires 1> h <- array(1:16,dim <- (2,8))Error: unexpected ',' in "h <- array(1:16,dim <- (2,"> h <- array(1:16,dim = (2,8))Error: unexpected ',' in "h <- array(1:16,dim = (2,"> h <- array(1:16,dim = c(2,8))> h [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 3 5 7 9 11 13 15 [2,] 2 4 6 8 10 12 14 16
> 查看全部 -
> w <- matrix(1:12,nrow = 2,ncol = 6)> w [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 5 7 9 11 [2,] 2 4 6 8 10 12> dim(w)[1] 2 6> attributes(w)$dim [1] 2 6> j <- 1:10> dim(j) <- c(5,2)> j [,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10> rbind(w,j)Error in rbind(w, j) : number of columns of matrices must match (see arg 2)> p <- matrix(1:9,nrow = 3,ncol = 3)> p [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9> q <- matrix(-1:-9,nrow = 3,ncol = 3)> q [,1] [,2] [,3] [1,] -1 -4 -7 [2,] -2 -5 -8 [3,] -3 -6 -9> rbind(p,q) [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 [4,] -1 -4 -7 [5,] -2 -5 -8 [6,] -3 -6 -9> rbind(q,p) [,1] [,2] [,3] [1,] -1 -4 -7 [2,] -2 -5 -8 [3,] -3 -6 -9 [4,] 1 4 7 [5,] 2 5 8 [6,] 3 6 9
> 查看全部 -
> k <- matrix(3,4)> k [,1] [1,] 3 [2,] 3 [3,] 3 [4,] 3> l <- matrix(1:16,4)> l [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16> l <- matrix(1:16,4,4)> l [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16
查看全部 -
> y <- "hello world"> y[1] "hello world"> class(y)[1] "character"> y <- TRUE> y[1] TRUE> class(y)[1] "logical"> y <- 1+iError: object 'i' not found> y <- 1+2i> y[1] 1+2i> class(y)[1] "complex"> y <- 1-iError: object 'i' not found> y <- i+1Error: object 'i' not found> length(y)[1] 1> m <- 1+2i> length(m)[1] 1> x <- vector("number",length = 10)Error in vector("number", length = 10) : vector: cannot make a vector of mode 'number'.> x <- vector("numeric",length = 10)> x [1] 0 0 0 0 0 0 0 0 0 0> x <- vector("character",length = 10)> x [1] "" "" "" "" "" "" "" "" "" ""> x <- vector("character",length = 10)> x <- vector("character",length = 10)> x <- vector("character",length = 10)> x <- vector("character",length = 10)> x <- 1:16> x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16> x <- 1:100> x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [30] 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 [59] 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 [88] 88 89 90 91 92 93 94 95 96 97 98 99 100> x <- c(1,2,3,4)> t <- c(1,2,3)> t[1] 1 2 3> as.numeric(t)[1] 1 2 3> u <- c(1,3,5)> as.numeric(u)[1] 1 3 5> as.character(u)[1] "1" "3" "5"> u <- c(1,3,5)> #as.numeric(u)> as.character(u)[1] "1" "3" "5"> v <- c(100,200)> as.character(v)[1] "100" "200"> as.logical(v)[1] TRUE TRUE> b <- c(0,1,0)> as.logical(b)[1] FALSE TRUE FALSE> class(b)[1] "numeric"> class(v)[1] "numeric"> class(as.character(v))[1] "character"> names(b) <- c("上","中","下")> b上 中 下 0 1 0 > names(v) <- c("一班成绩","二班成绩")> v一班成绩 二班成绩 100 200
> 查看全部 -
sapply和split通常与lapply结合使用
查看全部 -
object.size(函数)可以查看函数大小,返回结果的单位是字节。
print(object.size(函数),units="Kb")可以将结果的单位变成Kb
查看全部 -
交叉表函数xtabs(Freq~变量1+变量2,数据)可以查看满足变量1和2的结果数,ftable()使结果变得扁平化
查看全部 -
any(is.na())返回逻辑元素,表示是否存在缺失值,sum(is.na())总结缺失值数量
all(条件)表示判断条件是否正确,返回逻辑元素
查看全部
举报