R中的Data.frames:名称自动补全? [英] Data.frames in R: name autocompletion?

查看:86
本文介绍了R中的Data.frames:名称自动补全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,这很简单。我在R中看到以下行为:

Sorry if this is trivial. I am seeing the following behaviour in R:

> myDF <- data.frame(Score=5, scoreScaled=1)
> myDF$score ## forgot that the Score variable was capitalized
[1] 1

期望结果:返回NULL(甚至更好:抛出错误)。

Expected result: returns NULL (even better: throws error).

我已经搜索了此文件,但找不到有关此行为的任何讨论。有谁能够提供任何参考资料,这样做的理由以及是否有防止这种行为的理由?总的来说,我希望R的版本对它的变量更加严格,但是似乎永远不会发生...

I have searched for this, but was unable to find any discussion of this behaviour. Is anyone able to provide any references on this, the rationale on why this is done and if there is any way to prevent this? In general I would love a version of R that is a little stricter with its variables, but it seems that will never happen...

推荐答案

$ 运算符仅需要数据框名称的第一个唯一部分对其进行索引。例如,

The $ operator needs only the first unique part of a data frame name to index it. So for example:

> d <- data.frame(score=1, scotch=2)
> d$sco
NULL
> d$scor
[1] 1

一种避免此行为的方法是使用[[]]运算符,其行为将如下所示:

A way of avoiding this behavior is to use the [[]] operator, which will behave like so:

> d <- data.frame(score=1, scotch=2)
> d[['scor']]
NULL
> d[['score']]
[1] 1

我希望这会有所帮助

干杯!

这篇关于R中的Data.frames:名称自动补全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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