r - R 是否忽略数据框中以点开头的变量名称扩展?

标签 r dataframe names

我有一个数据框,其中一些变量名称包含“。”延期。如果我尝试在没有完整变量名的情况下访问它,R 似乎会忽略这个扩展并给我这个变量。是什么导致了这种情况/为什么会发生?下面是我的问题的一个小例子。

y <- rnorm(100)
x <- rlnorm(100)

data <- cbind.data.frame(y,x)

colnames(data) <- c("y.rnorm","x.rlnorm")

# these both return the same thing
data$y
data$y.rnorm

最佳答案

R 设置为按设计为部分匹配提供结果。

阅读栏目 3.4 & 4.3 R语言定义。

3.4.1 Character. The strings in i are matched against the names attribute of x and the resulting integers are used. For [[ and $ partial matching is used if exact matching fails, so x$aa will match x$aabb if x does not contain a component named "aa" and "aabb" is the only name which has prefix "aa". For [[, partial matching can be controlled via the exact argument which defaults to NA indicating that partial matching is allowed, but should result in a warning when it occurs. Setting exact to TRUE prevents partial matching from occurring, a FALSE value allows it and does not issue any warnings. Note that [ always requires an exact match. The string "" is treated specially: it indicates ‘no name’ and matches no element (not even those without a name). Note that partial matching is only used when extracting and not when replacing.





4.3.2 Partial matching on tags. Each remaining named supplied argument is compared to the remaining formal arguments using partial matching. If the name of the supplied argument matches exactly with the first part of a formal argument then the two arguments are considered to be matched. It is an error to have multiple partial matches. Notice that if f <- function(fumble, fooey) fbody, then f(f = 1, fo = 2) is illegal, even though the 2nd actual argument only matches fooey. f(f = 1, fooey = 2) is legal though since the second argument matches exactly and is removed from consideration for partial matching. If the formal arguments contain ‘...’ then partial matching is only applied to arguments that precede it.



更新

正如 Uwe 所指出的,可能存在对 R 语言定义的未决更新,因为 [[ 的行为部分匹配已更改。浏览 R News显示以下内容在 3.1.0 版本中已弃用和失效:

Partial matching when using the $ operator on data frames now throws a warning and may become defunct in the future. If partial matching is intended, replace foo$bar by foo[["bar", exact = FALSE]]

关于r - R 是否忽略数据框中以点开头的变量名称扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47583897/

相关文章:

R -- 按组将日期范围扩展到面板数据

在 grid.xaxis 中旋转标签

scala - 将Spark Scala App的同一数据框中的“天数”列添加到“日期”列

python - 为什么 pandas.DataFrame.apply 打印出垃圾?

r - 将嵌套列表中的名称分配给它的子组件

php - 如何检查字符串是否可以用作 PHP 中的变量名?

r - 如何防止 kable 在页面之间拆分?

r - 直接从 R 发布要点?

python - Pandas 合并两个数据框,一个包含另一个数据框的列值

r - 将命名向量转换为列表但保留向量名称