R 将 csv 数字列表(逗号分隔值,即 1, 2, 3
)包装在 c()
函数中,该函数是 R 核心库的一部分,它将 csv 数字列表转换为向量。
这些向量看起来像 Ruby 或 Java 数组,不同之处在于这些 csv 列表包含在 c()
中,而不是 []
中。看起来数组实际上是向量的子集。这是真的吗?如果是这样,这对于数组和矩阵意味着什么?
我发现关于该主题的一个讨论指出数组是静态向量。但在 Ruby 中,数组不是静态的。在 Ruby 中,数组是向量?
最佳答案
是的,在 R 中,数组只是一个带有给出数组维度的属性的向量。
来自?数组
:
Details:
An array in R can have one, two or more dimensions. It is simply a vector which is stored with additional attributes giving the dimensions (attribute ‘"dim"’) and optionally names for those dimensions (attribute ‘"dimnames"’).
A two-dimensional array is the same thing as a ‘matrix’.
One-dimensional arrays often look like vectors, but may be handled differently by some functions: ‘str’ does distinguish them in recent versions of R.
The ‘"dim"’ attribute is an integer vector of length one or more containing non-negative values: the product of the values must match the length of the array.
也许您自己了解这一点的最简单方法是查看向量、矩阵和高维数组,如下所示:
a <- array(1:12, dim=c(2,2,3))
m <- matrix(1:4, ncol=2)
v <- c(1,2)
is(a)
# [1] "array" "matrix" "structure" "vector" "vector"
is(m)
# [1] "matrix" "array" "structure" "vector"
is(v)
# [1] "numeric" "vector"
attributes(a)
# $dim
# [1] 2 2 3
attributes(m)
# $dim
# [1] 2 2
attributes(v)
# NULL
## Finally, try this
v <- 1:12
dim(v) <- c(2,2,3)
v
关于ruby - Ruby 和类似类型语言中的数组是向量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753419/