r - 当我在数据框中仅使用列名的初始部分时,为什么 R 不会引发错误?

标签 r dataframe columnname

我有一个包含各种列的数据框以及 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 commentBenBolker'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 to x[["name", exact = FALSE]]. Also, the partial matching behavior of "[[" can be controlled using the exact 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 argument exact).

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 by options(warnPartialMatchDollar = TRUE).


请注意,手册中有丰富的信息,请确保您完全消化了它们。我格式化了内容,在相关的后面添加了 Stack Overflow 线程。

链接由 phiver's comment 提供值得长期阅读。

关于r - 当我在数据框中仅使用列名的初始部分时,为什么 R 不会引发错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056962/

相关文章:

r - 将函数应用于由分类变量的所有可能组合子集的数据框

python - 用条件过滤pandas df

python - 与列表比较后在 Pandas 数据框中插入行

r - 将 as.vector 应用于矩阵切片时保留列名

根据R中列表的名称重命名数据框的列

javascript - Dygraphs:适用于 axisLabelFormatter 但不适用于 valueFormatter 的 JS 函数

r - 将整数序列 1, 2, 3, ... 转换为相应的字符串序列 A, B, C,

R/ Shiny 用户注册/认证

python - 在 pandas DataFrame 中查找满足多个逻辑( bool )条件的组

r - 将名称追加或粘贴到R中的列名称