字符串中NA的传染性和特殊性

  1. NA ^ 0
  2. #> [1] 1
  3. NA | TRUE
  4. #> [1] TRUE
  5. NA & FALSE
  6. #> [1] FALSE

3. Attributes

  1. matrices, arrays, factors, or date-times.是在atomic vectors的基础上添加属性完成的
  2. Individual attributes can be retrieved and modified with attr(), or retrieved en masse with attributes(), and set en masse with structure(). ```r a <- 1:3 attr(a, “x”) <- “abcdef” attr(a, “x”)

    > [1] “abcdef”

attr(a, “y”) <- 4:6 str(attributes(a))

> List of 2

> $ x: chr “abcdef”

> $ y: int [1:3] 4 5 6

Or equivalently

a <- structure( 1:3, x = “abcdef”, y = 4:6 ) str(attributes(a))

> List of 2

> $ x: chr “abcdef”

> $ y: int [1:3] 4 5 6

  1. 属性通常应该被认为是短暂的。例如,大多数操作都会丢失大多数属性:
  2. ```r
  3. attributes(a[1])
  4. #> NULL
  5. attributes(sum(a))
  6. #> NULL

如果你想要保留其他属性,您需要创建自己的 S3 类

3.1 summary

Vector Matrix Array
names() rownames()
colnames()
dimnames()
length() nrow()
ncol()
dim()
c() rbind()
cbind()
abind::abind()
t() aperm()
is.null(dim(x)) is.matrix() is.array()

4. S3 atomic vectors

最重要的向量属性之一是class,拥有一个类属性(class)会将一个对象变成一个S3 对象,
S3 对象被传递给一个通用函数(generic function)时,它的行为将与一个常规向量不同。

4.1

four important S3 vectors used in base R:

  • Categorical data, where values come from a fixed set of levels recorded in factor vectors.
  • Dates (with day resolution), which are recorded in Date vectors.
  • Date-times (with second or sub-second resolution), which are stored in POSIXct vectors.
  • Durations, which are stored in difftime vectors.
  • image.png