R语言实战-数据类型-5数据类型相关函数

数据类型观察函数

length

dim

str #mean structure here

names

mode

typeof

class

测试数据

 1 a<-c(1,2,3,4,5)
 2 b<-c('a','b','c','d')
 3 c<-c(T,F,T,T)
 4 
 5 d<-matrix(1:25,nrow = 5,byrow = T)
 6 
 7 e<-data.frame(b,c)
 8 
 9 f<-array(1:100,dim = c(2,4,5))
10 
11 g<-list(a,b,c,d,e,f)

测试方法

 1 func<-function(a){
 2   
 3   cat('object is:',as.character(a),'
')
 4   cat('type is:',typeof(a),'
')
 5   cat('length is:',length(a),'
')
 6   cat('dimension is:',dim(a),'
')
 7   cat('structure is:',str(a),'
')
 8   cat('class is:',class(a),'
')
 9   cat('mode is:',mode(a),'
')
10   cat('name is:',names(a),'
')
11   cat('head element is:',head(a),'
')
12   cat('tail element is:',tail(a),'
')
13 }

测试代码

1 func(a)
2 func(b)
3 func(c)
4 func(d)
5 func(e)
6 func(f)
7 func(g)

测试结果

 1 > func(a)
 2 object is: 1 2 3 4 5 
 3 type is: double 
 4 length is: 5 
 5 dimension is: 
 6  num [1:5] 1 2 3 4 5
 7 structure is: 
 8 class is: numeric 
 9 mode is: numeric 
10 name is: 
11 head element is: 1 2 3 4 5 
12 tail element is: 1 2 3 4 5 
13 > func(b)
14 object is: a b c d 
15 type is: character 
16 length is: 4 
17 dimension is: 
18  chr [1:4] "a" "b" "c" "d"
19 structure is: 
20 class is: character 
21 mode is: character 
22 name is: 
23 head element is: a b c d 
24 tail element is: a b c d 
25 > func(c)
26 object is: TRUE FALSE TRUE TRUE 
27 type is: logical 
28 length is: 4 
29 dimension is: 
30  logi [1:4] TRUE FALSE TRUE TRUE
31 structure is: 
32 class is: logical 
33 mode is: logical 
34 name is: 
35 head element is: TRUE FALSE TRUE TRUE 
36 tail element is: TRUE FALSE TRUE TRUE 
37 > func(d)
38 object is: 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 
39 type is: integer 
40 length is: 25 
41 dimension is: 5 5 
42  int [1:5, 1:5] 1 6 11 16 21 2 7 12 17 22 ...
43 structure is: 
44 class is: matrix 
45 mode is: numeric 
46 name is: 
47 head element is: 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 
48 tail element is: 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 
49 > func(e)
50 object is: 1:4 c(TRUE, FALSE, TRUE, TRUE) 
51 type is: list 
52 length is: 2 
53 dimension is: 4 2 
54 'data.frame':    4 obs. of  2 variables:
55  $ b: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
56  $ c: logi  TRUE FALSE TRUE TRUE
57 structure is: 
58 class is: data.frame 
59 mode is: list 
60 name is: b c 
61 head element is: Error in cat("head element is:", head(a), "
") : argument 2 (type 'list') cannot be handled by 'cat'
62 > func(f)
63 object is: 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 31 32 33 34 35 36 37 38 39 40 
64 type is: integer 
65 length is: 40 
66 dimension is: 2 4 5 
67  int [1:2, 1:4, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
68 structure is: 
69 class is: array 
70 mode is: numeric 
71 name is: 
72 head element is: 1 2 3 4 5 6 
73 tail element is: 35 36 37 38 39 40 
74 > func(g)
75 object is: c(1, 2, 3, 4, 5) c("a", "b", "c", "d") c(TRUE, FALSE, TRUE, TRUE) c(1, 6, 11, 16, 21, 2, 7, 12, 17, 22, 3, 8, 13, 18, 23, 4, 9, 14, 19, 24, 5, 10, 15, 20, 25) list(b = 1:4, c = c(TRUE, FALSE, TRUE, TRUE)) 1:40 
76 type is: list 
77 length is: 6 
78 dimension is: 
79 List of 6
80  $ : num [1:5] 1 2 3 4 5
81  $ : chr [1:4] "a" "b" "c" "d"
82  $ : logi [1:4] TRUE FALSE TRUE TRUE
83  $ : int [1:5, 1:5] 1 6 11 16 21 2 7 12 17 22 ...
84  $ :'data.frame':    4 obs. of  2 variables:
85   ..$ b: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
86   ..$ c: logi [1:4] TRUE FALSE TRUE TRUE
87  $ : int [1:2, 1:4, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
88 structure is: 
89 class is: list 
90 mode is: list 
91 name is: 
92 head element is: Error in cat("head element is:", head(a), "
") : argument 2 (type 'list') cannot be handled by 'cat'
原文地址:https://www.cnblogs.com/qianheng/p/10803425.html