问题描述
由于 R 向量索引从 1 开始,所以允许索引为零有什么意义:
Since R vector indices start at 1, what is the point of allowing indexing with zero:
rivers[0]
#numeric(0)
这将返回一个零长度向量.为什么不是错误?这有什么用?
This returns a zero length vector. Why not an error? How is this useful?
推荐答案
通常出现的用例并不多.我实际上有一个我正在为基于模板的验证开发的包强>.例如,如果我想创建iris
数据框的模板,我可以使用:
There aren't many use cases that crop up typically. I actually have one in the context of a package I'm developing for template based validations. For example, if I want to create a template of the iris
data frame, I can use:
iris[0, ]
产生(注意,显示了 str(iris[0, ])
的输出):
Produces (note, showing output of str(iris[0, ])
):
'data.frame': 0 obs. of 5 variables:
$ Sepal.Length: num
$ Sepal.Width : num
$ Petal.Length: num
$ Petal.Width : num
$ Species : Factor w/ 3 levels "setosa","versicolor",..:
我基本上抽象了数据框的结构.我有所有的列定义,但没有数据.
I've essentially abstracted the structure of the data frame. I have all the column definitions, but none of the data.
这篇关于在子集化时允许零索引有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!