R中向量的定义 [英] Definition of vector in R

查看:37
本文介绍了R中向量的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到一个向量是一系列相同基本类型的数据元素. 那么我们将在下面的代码中调用什么a(因为它包含数字和字符):

a = c(1,"b")is.vector(a)[1] 真

那么向量的定义是错误的吗?我参考了本教程.

解决方案

本教程简化,但可能会引起混淆.它的定义描述了基本向量类型",但也有通用向量".

语言定义(其中你应该学习):

<块引用>

2.1.1 向量

向量可以被认为是包含数据的连续单元格.细胞通过索引操作(例如 x[5])访问.更多细节在索引中给出.

R 有六种基本(‘原子’)向量类型:逻辑、整数、实数、复杂,字符串(或字符)和原始.模式和存储模式下表列出了不同的向量类型.

typeof mode storage.mode逻辑逻辑逻辑整数 数字 整数双数字双复杂的复杂的性格性格性格生的 生的

<块引用>

单个数字,例如 4.2,而字符串,例如四点二"仍然是长度为1的向量;没有更多的基本类型.长度为零的向量是可能的(而且很有用).

2.1.2 列表

列表(通用向量")是另一种数据存储.列表有元素,每个元素都可以包含任何类型的 R对象,即列表的元素不必相同类型.列表元素通过三种不同的索引访问操作.这些在索引中有详细说明.

列表是向量,基本的向量类型被称为需要排除列表的原子向量.

来自help("is.vector"):

<块引用>

如果 mode = "any",is.vector 可能为原子模式返回 TRUE,列出和表达.对于任何模式,如果 x 有任何模式,它将返回 FALSE除名称外的属性.[...]

(表达式基本上就是一个list.)

<块引用>

注意因子不是向量;is.vector 返回 FALSE,as.vector 将一个因子转换为 mode = "any" 的字符向量.

最后,正如@Henrik 指出的那样,c 将所有参数强制为同一类型.

I learnt that a vector is a sequence of data elements of the same basic type. Then what will we call a in the following code (as it contains both numeric and charater):

a = c(1,"b")
is.vector(a)
[1] TRUE

So is the definition of vector wrong? I referred this tutorial.

解决方案

The tutorial simplifies and that can cause confusion. Its definition describes "basic vector types", but there are also "generic vectors".

From the language definition (which you should study):

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.

From 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. [...]

(An expression is basically a list.)

Note that factors are not vectors; is.vector returns FALSE and as.vector converts a factor to a character vector for mode = "any".

Finally, as @Henrik points out, c coerces all arguments to the same type.

这篇关于R中向量的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆