2.2.4 数据框
先来创建下面操作需要用到的矩阵和数据框
b3<-matrix(1:15,3,5)构建矩阵b3,1到15的数值,3行,5列。b3显示b3。[,1][,2][,3][,4][,5][1,]1 4 7 10 13[2,]2 5 8 11 14[3,]3 6 9 12 15name <- c("one","two","three","four","five","six","seven","eight")构建字符型向量name。factor <- c("a","b","a","b","a","b","a","b")values<- c(1,2,3,4,5,6,7,8)构建数值型向量values。values2 <- c(8,7,6,5,4,3,2,1)构建数值型向量values2。A <- data.frame(name, factor,values, values2, row.names = c("r1","r2","r3","r4","r5","r6","r7","r8"))创建数据框A。A显示结果A。name factor values values2r1 one a 1 8r2 two b 2 7r3 three a 3 6r4 four b 4 5r5 five a 5 4r6 six b 6 3r7 seven a 7 2r8 eight b 8 1str(A)查看数据结构data.frame: 8 obs. of 4 variables:$ name : chr "one" "two" "three" "four" ...$ factor : chr "a" "b" "a" "b" ...$ values : num 1 2 3 4 5 6 7 8$ values2: num 8 7 6 5 4 3 2 1
4.数据框操作
is.data.frame(A)is.data.frame()用于判定数据是否是数据框。若是,返回值为TURE,不是,返回FALSE。[1] TRUEb3<-as.data.frame(b3)as.data.frame()用于将数据转换为数据框。这里将矩阵b3转为数据框。b3显示结果。V1 V2 V3 V4 V51 1 4 7 10 132 2 5 8 11 143 3 6 9 12 15colnames(b3)[3] <-"value"修改数据框y3第3列列名为value。要修改行名用rownames(A)[行号,列号] <- 新名称。b3显示结果。V1 V2 value V4 V51 1 4 7 10 132 2 5 8 11 143 3 6 9 12 15colnames(b3) <- c("列1","列2","列3","列4","列5")对数据框b3的所有列重命名。b3显示结果。列1 列2 列3 列4 列51 1 4 7 10 132 2 5 8 11 143 3 6 9 12 15A显示数据框A。name factor values values2r1 one a 1 8r2 two b 2 7r3 three a 3 6r4 four b 4 5r5 five a 5 4r6 six b 6 3r7 seven a 7 2r8 eight b 8 1t(A)数据框转置。r1 r2 r3 r4 r5 r6 r7 r8name"one""two""three""four""five""six""seven""eight"factor"a""b""a""b""a""b""a""b"values"1""2""3""4""5""6""7""8"values2"8""7""6""5""4""3""2""1"A[1,]<-c(1,1,1,1)修改数据框A第1行的数据。A显示结果A。name factor values values2r1 1 1 1 1r2 two b 2 7r3 three a 3 6r4 four b 4 5r5 five a 5 4r6 six b 6 3r7 seven a 7 2r8 eight b 8 1A[,2]<-c(2,4,6,8,10,12,14,16)修改数据框A第2列的数据。A显示结果A。name factor values values2r1 1 2 1 1r2 two 4 2 7r3 three 6 3 6r4 four 8 4 5r5 five 10 5 4r6 six 12 6 3r7 seven 14 7 2r8 eight 16 8 1A[1,1] <-"one"修改第1列第1行的值。A显示结果A。name factor values values2r1 one 2 1 1r2 two 4 2 7r3 three 6 3 6r4 four 8 4 5r5 five 10 5 4r6 six 12 6 3r7 seven 14 7 2r8 eight 16 8 1A$values<- as.character(A$values)将数据框A中values列类型从数值型改为字符型。class(A$values)查看结果。[1] "character"A$values <- as.numeric(A$values)从字符型转换会数值型。class(A$values)查看结果。[1] "numeric"A[9,] <- c("nine","a",18,0)给数据框A增加第9行。A显示结果。name factor values values2r1 one 2 1 1r2 two 4 2 7r3 three 6 3 6r4 four 8 4 5r5 five 10 5 4r6 six 12 6 3r7 seven 14 7 2r8 eight 16 8 19 nine a 18 0A[,4]<-c(1,3,5,7,9,11,13,15,17)增加新列。A显示结果。name factor values values2r1 one 2 1 1r2 two 4 2 3r3 three 6 3 5r4 four 8 4 7r5 five 10 5 9r6 six 12 6 11r7 seven 14 7 13r8 eight 16 8 159 nine a 18 17A$values1<-c(1,1,2,2,3,3,4,4,5)增加新列,列名为values2。A显示结果。name factor values values2 values1r1 one 2 1 1 1r2 two 4 2 3 1r3 three 6 3 5 2r4 four 8 4 7 2r5 five 10 5 9 3r6 six 12 6 11 3r7 seven 14 7 13 4r8 eight 16 8 15 49 nine a 18 17 5A<- data.frame(A, values3 = c("a","b","a","b","a","b","a","b","a"))在原有数据框的基础上使用data.frame()函数新增列。A显示结果。name factor values values2 values1 values3r1 one 2 1 1 1 ar2 two 4 2 3 1 br3 three 6 3 5 2 ar4 four 8 4 7 2 br5 five 10 5 9 3 ar6 six 12 6 11 3 br7 seven 14 7 13 4 ar8 eight 16 8 15 4 b9 nine a 18 17 5 aA<- A[-9,]删除第9行。A显示结果。name factor values values2 values1 values3r1 one 2 1 1 1 ar2 two 4 2 3 1 br3 three 6 3 5 2 ar4 four 8 4 7 2 br5 five 10 5 9 3 ar6 six 12 6 11 3 br7 seven 14 7 13 4 ar8 eight 16 8 15 4 bA<- A[,-4]删除第4列数据。A显示结果。name factor values values1 values3r1 one 2 1 1 ar2 two 4 2 1 br3 three 6 3 2 ar4 four 8 4 2 br5 five 10 5 3 ar6 six 12 6 3 br7 seven 14 7 4 ar8 eight 16 8 4 b
5.数据框排序
A[order(A$values1),]数据框排序,按照values1进行,order函数默认是升序。name factor values values1 values3r1 one 2 1 1 ar2 two 4 2 1 br3 three 6 3 2 ar4 four 8 4 2 br5 five 10 5 3 ar6 six 12 6 3 br7 seven 14 7 4 ar8 eight 16 8 4 bA[order(-A$values1),]对数据框A以values1变量降序排列。name factor values values1 values3r7 seven 14 7 4 ar8 eight 16 8 4 br5 five 10 5 3 ar6 six 12 6 3 br3 three 6 3 2 ar4 four 8 4 2 br1 one 2 1 1 ar2 two 4 2 1 bA[order(A$name, decreasing = T),]当排序依据变量为数值型时,前面加-就是按照降序排列,但是当变量为字符型时,就需在order函数中指定参数decreasing,T为TRUE,即降序。本例中为英文字母,所以降序按照英文字母首字母排序。name factor values values1 values3r2 two 4 2 1 br3 three 6 3 2 ar6 six 12 6 3 br7 seven 14 7 4 ar1 one 2 1 1 ar4 four 8 4 2 br5 five 10 5 3 ar8 eight 16 8 4 bA[order(A$values, -A$values1),]以values升序排列,以values1为降序排列。name factor values values1 values3r1 one 2 1 1 ar2 two 4 2 1 br3 three 6 3 2 ar4 four 8 4 2 br5 five 10 5 3 ar6 six 12 6 3 br7 seven 14 7 4 ar8 eight 16 8 4 bsort(A$values3)也可以用sort()函数进行排序。使用方法与order基本相同。[1]"a""a""a""a""b""b""b""b"
从数学中最基本的加减乘除运算开始(小学课本的这些字你都读对了吗) 英国被俄罗斯反制裁!俄罗斯拒绝发射OneWeb,Space X也见死不救 为什么大部分枪都是黑色的? 1到100用英语怎么说 哆来咪的英文歌词(哆来咪的英文歌词是什么?)