我有一个包含各种列的数据框以及 sender_bank_flag
.我在我的数据框上运行了以下两个查询。sum(s_50k_sample$sender_bank_flag, na.rm=TRUE)
sum(s_50k_sample$sender_bank, na.rm=TRUE)
即使没有像 sender_bank
这样的列,我也从两个查询中得到了相同的输出。在我的数据框中。我预计第二个代码会出错。不知道R有这样的功能!有谁知道这个功能到底是什么以及如何更好地利用它?
最佳答案
可能值得将所有评论扩充为答案。
两者 my comment和 BenBolker's指向文档页面 ?Extract
:
下 递归(类列表)对象:
Both
"[["
and"$"
select a single element of the list. The main difference is that"$"
does not allow computed indices, whereas"[["
does.x$name
is equivalent tox[["name", exact = FALSE]]
. Also, the partial matching behavior of"[["
can be controlled using theexact
argument.
下 字符索引:
Character indices can in some circumstances be partially matched (see
?pmatch
) to the names or dimnames of the object being subsetted (but never for subassignment). Unlike S (Becker et al p. 358), R never uses partial matching when extracting by"["
, and partial matching is not by default used by"[["
(see argumentexact
).Thus the default behaviour is to use partial matching only when extracting from recursive objects (except environments) by
"$"
. Even in that case, warnings can be switched on byoptions(warnPartialMatchDollar = TRUE)
.
请注意,手册中有丰富的信息,请确保您完全消化了它们。我格式化了内容,在相关的后面添加了 Stack Overflow 线程。
链接由 phiver's comment 提供值得长期阅读。
关于r - 当我在数据框中仅使用列名的初始部分时,为什么 R 不会引发错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056962/