R中向量的定义
Definition of vector in R
我了解到 a vector is a sequence of data elements of the same basic type.
那么我们将在下面的代码中调用什么 a
(因为它包含数字和字符):
a = c(1,"b")
is.vector(a)
[1] TRUE
那么vector的定义是不是错了?我提到了 this tutorial.
教程进行了简化,这可能会造成混淆。它的定义描述了"basic vector types",但也有"generic vectors".
来自language definition(你应该学习):
2.1.1 Vectors
Vectors can be thought of as contiguous cells containing data. Cells
are accessed through indexing operations such as x[5]. More details
are given in Indexing.
R has six basic (‘atomic’) vector types: logical, integer, real,
complex, string (or character) and raw. The modes and storage modes
for the different vector types are listed in the following table.
typeof mode storage.mode
logical logical logical
integer numeric integer
double numeric double
complex complex complex
character character character
raw raw raw
Single numbers, such as 4.2,
and strings, such as "four point two" are still vectors, of length 1;
there are no more basic types. Vectors with length zero are possible
(and useful).
2.1.2 Lists
Lists (“generic vectors”) are another kind of data storage. Lists have
elements, each of which can contain any type of R
object, i.e. the elements of a list do not have to be of the same
type. List elements are accessed through three different indexing
operations. These are explained in detail in Indexing.
Lists are vectors, and the basic vector types are referred to as
atomic vectors where it is necessary to exclude lists.
来自 help("is.vector")
:
If mode = "any", is.vector may return TRUE for the atomic modes, list
and expression. For any mode, it will return FALSE if x has any
attributes except names. [...]
(expression
基本上是 list
。)
Note that factors are not vectors; is.vector returns FALSE and as.vector converts a factor to a character vector for mode = "any".
最后,正如@Henrik 指出的那样,c
将所有参数强制转换为同一类型。
实际上,在您的示例中,“1”将被 R 视为一个字符。
a<-c(1,"b")
typeof(a[1])
[1] "character"
我了解到 a vector is a sequence of data elements of the same basic type.
那么我们将在下面的代码中调用什么 a
(因为它包含数字和字符):
a = c(1,"b")
is.vector(a)
[1] TRUE
那么vector的定义是不是错了?我提到了 this tutorial.
教程进行了简化,这可能会造成混淆。它的定义描述了"basic vector types",但也有"generic vectors".
来自language definition(你应该学习):
2.1.1 Vectors
Vectors can be thought of as contiguous cells containing data. Cells are accessed through indexing operations such as x[5]. More details are given in Indexing.
R has six basic (‘atomic’) vector types: logical, integer, real, complex, string (or character) and raw. The modes and storage modes for the different vector types are listed in the following table.
typeof mode storage.mode
logical logical logical
integer numeric integer
double numeric double
complex complex complex
character character character
raw raw raw
Single numbers, such as 4.2, and strings, such as "four point two" are still vectors, of length 1; there are no more basic types. Vectors with length zero are possible (and useful).
2.1.2 Lists
Lists (“generic vectors”) are another kind of data storage. Lists have elements, each of which can contain any type of R object, i.e. the elements of a list do not have to be of the same type. List elements are accessed through three different indexing operations. These are explained in detail in Indexing.
Lists are vectors, and the basic vector types are referred to as atomic vectors where it is necessary to exclude lists.
来自 help("is.vector")
:
If mode = "any", is.vector may return TRUE for the atomic modes, list and expression. For any mode, it will return FALSE if x has any attributes except names. [...]
(expression
基本上是 list
。)
Note that factors are not vectors; is.vector returns FALSE and as.vector converts a factor to a character vector for mode = "any".
最后,正如@Henrik 指出的那样,c
将所有参数强制转换为同一类型。
实际上,在您的示例中,“1”将被 R 视为一个字符。
a<-c(1,"b") typeof(a[1]) [1] "character"