Summary and Example of R Language Attribute Knowledge Points

  • 2021-10-13 08:04:33
  • OfStack

Attributes (attribute): Properties of objects in R

Attribute describes what is represented and how R interprets the object

Most of the time, the only difference between two objects is that their attributes are different

Common properties

属性 描述
class 对象的类
comment 对象的注释,1般用于描述对象的含义
dim 对象的维度
dimnames 与对象的每个维度相关的名字
names 返回对象的名字属性.返回结果取决于对象的类型.对于数据框对象会返回数据框的列名;对于数组会返回数组中被命名元素的名字
row,names 对象的行名(dimnames相关)
tsp 对象的起始点,对于时间序列对象有用
levels 因子型变量的水平

Standard method for querying properties of objects in R:

For the object x and the attribute a: 1 Generally, the a attribute of x can be queried through a (x)

In most cases, there is a ready-made function in R to get or change object properties

(This method of changing object properties overrides the old properties of the object in the current environment, but does not affect variable properties in the closed environment.)


m = matrix(data = 1:12, nrow = 4, ncol = 3,
      dimnames = list(c("r1", "r2", "r3", "r4"),
              c("c1", "c2", "c3")))

With the attributes function, you can get a list containing all the attributes of the object


> attributes(m)
$dim
[1] 4 3

$dimnames
$dimnames[[1]]
[1] "r1" "r2" "r3" "r4"

$dimnames[[2]]
[1] "c1" "c2" "c3"

> dim(m)
[1] 4 3
> dimnames(m)
[[1]]
[1] "r1" "r2" "r3" "r4"

[[2]]
[1] "c1" "c2" "c3"

> colnames(m)
[1] "c1" "c2" "c3"
> rownames(m)
[1] "r1" "r2" "r3" "r4"

You can transform the matrix into objects of other classes by changing properties

For example, removing dimension attributes of an object


> dim(m) <- NULL
> m
 [1] 1 2 3 4 5 6 7 8 9 10 11 12
 > class(m)
[1] "integer"
> typeof(m)
[1] "integer"

Create an array


> (a <- array(1:12, dim = c(3:4)))
   [,1] [,2] [,3] [,4]
[1,]  1  4  7  10
[2,]  2  5  8  11
[3,]  3  6  9  12

Define a vector containing the same object


> (b <- 1:12)
 [1] 1 2 3 4 5 6 7 8 9 10 11 12

Question: Are these two objects equivalent in R?


> a == b
   [,1] [,2] [,3] [,4]
[1,] TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE

Each element of the array reflects the result of comparing the elements of two objects

all. equal function in R

Use to compare the data of two objects and Dimension 1 to identify whether the two objects are almost identical, and if they are not identical, the reason will be returned


> all.equal(a, b)
[1] "Attributes: < Modes: list, NULL >"          "Attributes: < Lengths: 1, 0 >"            
[3] "Attributes: < names for target but not for current >" "Attributes: < current is not list-like >"      
[5] "target is matrix, current is numeric" 

You can use the identical function if you just check whether two objects are completely identical, but you don't care about the reason


> identical(a, b)
[1] FALSE

Class

For simple types, their classes and types are closely related

For compliant objects, the two may be different

Sometimes, the classes of objects are listed with properties.

However, classes are hidden for certain types, such as matrices and arrays.

You can use the class function to determine the class of an object

You can use the typeof function to view the basic types of objects

You can change the class to which an R object belongs.

For example, factor vector is converted to integer array, integer array is converted to factor


> attributes(m)
$dim
[1] 4 3

$dimnames
$dimnames[[1]]
[1] "r1" "r2" "r3" "r4"

$dimnames[[2]]
[1] "c1" "c2" "c3"
0

> attributes(m)
$dim
[1] 4 3

$dimnames
$dimnames[[1]]
[1] "r1" "r2" "r3" "r4"

$dimnames[[2]]
[1] "c1" "c2" "c3"
1

When calling the class function or the typedef function, there are some objects that need to be referenced to prevent them from being executed at the time of call

For example, you want to query the symbol x instead of the type of the object pointed to by x


> attributes(m)
$dim
[1] 4 3

$dimnames
$dimnames[[1]]
[1] "r1" "r2" "r3" "r4"

$dimnames[[2]]
[1] "c1" "c2" "c3"
2

But not all types of objects can take this action

For example: any objects,... objects, string objects, and promise objects in R cannot be isolated

(Viewing the type of a promise object requires execution of the promise object, and this 1 procedure converts it to a normal object, making it impossible to query its type information.)


Related articles: