R中的class函数有助于我们了解对象的类型,例如,数据帧的class输出是整数,而同一对象的typeof是list,因为数据帧作为列表存储在内存中,但它们表示为一个数据框。查看以下示例,了解多种类型的对象,以了解不同之处。
x1<-rpois(20,2) typeof(x1)输出结果
[1] "integer"
等级(x1)
[1] "integer"
df1<-data.frame(x1) df1
x1 1 1 2 4 3 1 4 0 5 2 6 2 7 4 8 2 9 3 10 4 11 1 12 4 13 0 14 5 15 2 16 2 17 0 18 4 19 3 20 3
typeof(df1)
[1] "list"
等级(x1)
[1] "integer"
M<-matrix(rnorm(40),ncol=2) M输出结果
[,1] [,2] [1,] 2.02437789 -0.9161853 [2,] -0.60108978 -0.8972007 [3,] 1.27916953 0.1017923 [4,] 1.06998017 1.4839931 [5,] 0.22298522 0.6160919 [6,] -0.29346341 -0.3975116 [7,] 2.07012097 -0.7900820 [8,] 0.36719470 -0.1100298 [9,] -0.69522122 -1.9198172 [10,] 2.07822428 0.2517532 [11,] -1.56267422 1.8295022 [12,] -1.07488221 1.2666054 [13,] -0.79381494 -1.0993693 [14,] -0.16027224 -1.1814177 [15,] 0.67561791 0.7309281 [16,] -1.40912018 -0.3307749 [17,] -0.77769513 0.5527600 [18,] 0.47050704 0.1075593 [19,] -0.46616151 -0.5079660 [20,] -0.01944371 0.1553333
typeof(M)
[1] "double"
class(M)
[1] "matrix" "array"