要将名称分配给vector的值,我们可以使用名称函数,而使用unname函数可以删除名称。例如,如果我们有一个向量x,其中的x元素具有名称,而我们想要删除这些元素的名称,则可以使用以下命令unname(x)。
> x1<-1:50 > names(x1)<-sample(LETTERS[1:26],50,replace=TRUE) > x1输出结果
G K N V P F F A P D L N K J V H S L F C M F H T I V 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 V R M K Y I N L N F Y A C U H P T Z Z L K E L Y 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> unname(x1)输出结果
[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] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> x2<-rnorm(50) > names(x2)<-1:50 > x2输出结果
1 2 3 4 5 6 0.320434495 0.697341366 -0.690851096 -0.270470664 0.627552623 -0.670351573 7 8 9 10 11 12 0.416694153 0.135341529 -0.816141877 0.019222881 2.384280752 0.463381965 13 14 15 16 17 18 0.457718741 -1.220411217 0.286602691 2.023790519 -1.085566255 0.152970726 19 20 21 22 23 24 -0.949894774 0.905011951 0.479692477 1.431115243 -0.177645423 0.088219179 25 26 27 28 29 30 0.741544429 -0.306062256 -1.050278580 0.068178519 0.007891684 1.299952984 31 32 33 34 35 36 -0.118316910 -0.521622484 -0.028755947 1.305090322 -0.822214412 0.873313547 37 38 39 40 41 42 1.292973504 0.241589346 -0.688928369 -0.861205190 -0.311398855 1.338483253 43 44 45 46 47 48 -1.094867950 0.599904648 1.379982149 0.407302328 -0.855550788 -0.132740032 49 50 -0.709800907 -1.346831730
> unname(x2)输出结果
[1] 0.320434495 0.697341366 -0.690851096 -0.270470664 0.627552623 [6] -0.670351573 0.416694153 0.135341529 -0.816141877 0.019222881 [11] 2.384280752 0.463381965 0.457718741 -1.220411217 0.286602691 [16] 2.023790519 -1.085566255 0.152970726 -0.949894774 0.905011951 [21] 0.479692477 1.431115243 -0.177645423 0.088219179 0.741544429 [26] -0.306062256 -1.050278580 0.068178519 0.007891684 1.299952984 [31] -0.118316910 -0.521622484 -0.028755947 1.305090322 -0.822214412 [36] 0.873313547 1.292973504 0.241589346 -0.688928369 -0.861205190 [41] -0.311398855 1.338483253 -1.094867950 0.599904648 1.379982149 [46] 0.407302328 -0.855550788 -0.132740032 -0.709800907 -1.346831730
> x3<-rpois(200,5) > x3输出结果
[1] 9 4 4 6 6 9 8 8 2 5 1 8 3 6 2 4 4 6 3 3 7 7 6 2 5 [26] 4 3 5 5 4 7 4 4 6 4 6 2 5 6 9 10 5 9 4 5 7 2 4 5 5 [51] 1 7 6 5 0 7 5 4 1 2 4 3 3 4 2 1 3 6 5 7 5 6 7 2 6 [76] 3 8 8 5 4 3 3 3 8 7 3 8 2 3 6 6 5 10 8 9 5 2 4 6 5 [101] 4 6 6 9 4 3 5 4 10 1 7 5 3 7 7 4 5 8 3 3 4 6 5 7 3 [126] 7 11 6 4 6 9 7 7 5 8 0 3 6 3 6 8 8 3 5 3 6 7 4 3 6 [151] 4 4 2 5 7 3 6 2 6 5 2 2 5 6 6 4 5 5 4 11 2 3 7 4 6 [176] 7 8 7 5 6 9 3 4 8 4 2 5 6 5 3 3 4 6 9 4 3 6 4 7 3
> names(x3)<-sample(0:1,200,replace=TRUE) > x3输出结果
1 0 0 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 0 0 1 1 1 0 0 1 9 4 4 6 6 9 8 8 2 5 1 8 3 6 2 4 4 6 3 3 7 7 6 2 5 4 1 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 1 1 1 0 0 0 0 1 3 5 5 4 7 4 4 6 4 6 2 5 6 9 10 5 9 4 5 7 2 4 5 5 1 7 1 0 0 1 0 0 0 0 1 0 0 1 1 1 0 0 1 1 1 0 1 0 0 1 1 0 6 5 0 7 5 4 1 2 4 3 3 4 2 1 3 6 5 7 5 6 7 2 6 3 8 8 1 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1 0 5 4 3 3 3 8 7 3 8 2 3 6 6 5 10 8 9 5 2 4 6 5 4 6 6 9 0 0 1 1 0 1 1 0 0 1 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 1 4 3 5 4 10 1 7 5 3 7 7 4 5 8 3 3 4 6 5 7 3 7 11 6 4 6 0 1 0 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 1 0 1 1 0 0 9 7 7 5 8 0 3 6 3 6 8 8 3 5 3 6 7 4 3 6 4 4 2 5 7 3 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 0 1 1 0 0 0 1 0 1 1 1 6 2 6 5 2 2 5 6 6 4 5 5 4 11 2 3 7 4 6 7 8 7 5 6 9 3 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 1 4 8 4 2 5 6 5 3 3 4 6 9 4 3 6 4 7 3
> unname(x3)输出结果
[1] 9 4 4 6 6 9 8 8 2 5 1 8 3 6 2 4 4 6 3 3 7 7 6 2 5 [26] 4 3 5 5 4 7 4 4 6 4 6 2 5 6 9 10 5 9 4 5 7 2 4 5 5 [51] 1 7 6 5 0 7 5 4 1 2 4 3 3 4 2 1 3 6 5 7 5 6 7 2 6 [76] 3 8 8 5 4 3 3 3 8 7 3 8 2 3 6 6 5 10 8 9 5 2 4 6 5 [101] 4 6 6 9 4 3 5 4 10 1 7 5 3 7 7 4 5 8 3 3 4 6 5 7 3 [126] 7 11 6 4 6 9 7 7 5 8 0 3 6 3 6 8 8 3 5 3 6 7 4 3 6 [151] 4 4 2 5 7 3 6 2 6 5 2 2 5 6 6 4 5 5 4 11 2 3 7 4 6 [176] 7 8 7 5 6 9 3 4 8 4 2 5 6 5 3 3 4 6 9 4 3 6 4 7 3
> x4<-runif(50,1,5) > x4输出结果
[1] 2.411009 2.211185 1.391928 3.346429 3.132086 2.716000 2.500084 4.117285 [9] 3.513299 1.386085 4.712126 3.166818 1.613602 2.093889 4.185800 4.324013 [17] 4.807602 1.487395 1.221009 2.858887 3.183079 4.247183 1.680618 4.842318 [25] 1.104707 2.152010 1.513501 1.246102 4.083861 2.301772 1.899124 3.533562 [33] 4.567207 1.676978 1.272463 2.817372 3.833719 3.158989 4.262898 2.931992 [41] 1.983175 3.732617 4.654672 2.758082 4.614849 4.046474 3.797411 1.515049 [49] 1.607818 3.386288
> names(x4)<-1:50 > x4输出结果
1 2 3 4 5 6 7 8 2.411009 2.211185 1.391928 3.346429 3.132086 2.716000 2.500084 4.117285 9 10 11 12 13 14 15 16 3.513299 1.386085 4.712126 3.166818 1.613602 2.093889 4.185800 4.324013 17 18 19 20 21 22 23 24 4.807602 1.487395 1.221009 2.858887 3.183079 4.247183 1.680618 4.842318 25 26 27 28 29 30 31 32 1.104707 2.152010 1.513501 1.246102 4.083861 2.301772 1.899124 3.533562 33 34 35 36 37 38 39 40 4.567207 1.676978 1.272463 2.817372 3.833719 3.158989 4.262898 2.931992 41 42 43 44 45 46 47 48 1.983175 3.732617 4.654672 2.758082 4.614849 4.046474 3.797411 1.515049 49 50 1.607818 3.386288
> unname(x4)输出结果
[1] 2.411009 2.211185 1.391928 3.346429 3.132086 2.716000 2.500084 4.117285 [9] 3.513299 1.386085 4.712126 3.166818 1.613602 2.093889 4.185800 4.324013 [17] 4.807602 1.487395 1.221009 2.858887 3.183079 4.247183 1.680618 4.842318 [25] 1.104707 2.152010 1.513501 1.246102 4.083861 2.301772 1.899124 3.533562 [33] 4.567207 1.676978 1.272463 2.817372 3.833719 3.158989 4.262898 2.931992 [41] 1.983175 3.732617 4.654672 2.758082 4.614849 4.046474 3.797411 1.515049 [49] 1.607818 3.386288