r - 语法无效名称的后果

标签 r naming-conventions read.table

read.table 系列(read.tableread.csvread.delim 等) 的参数 check.names 具有以下解释:

logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names. If necessary they are adjusted (by make.names) so that they are, and also to ensure that there are no duplicates.

假设我加载了一个包含语法无效的列名的数据框。除了必须使用 ` 字符按名称访问特定列之外,还有其他后果吗?

最佳答案

查看 help(make.names) 以了解它在做什么以及为什么。

A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as ".2way" are not valid, and neither are the reserved words.

The definition of a letter depends on the current locale, but only ASCII digits are considered to be digits.

The character "X" is prepended if necessary. All invalid characters are translated to ".". A missing value is translated to "NA". Names which match R keywords have a dot appended to them. Duplicated values are altered by make.unique.

最让你困惑的是空白列名(df$`` 给出错误)和重复的列名(df$val 将返回第一个仅val 列结果)。

除此之外,如果您将此 data.frame 传递给需要具有有效名称的 data.frame 的函数,您可能会收到错误,并且可能会收到难以检测的静默错误。

关于r - 语法无效名称的后果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46225103/

相关文章:

r - Snowball Stemmer 只词干最后一个词

javascript - JSON 可序列化变量的命名约定

swift - 私有(private)属性的命名约定

Read.csv 使一切都变得消极

r - 如何为数据框中的每两个字符添加一个空格

r - 使用 rpart 选择决策树修剪的 CP 值

读取水平组织的 CSV 文件

r - 如何从逐步回归中提取公式?

读取带有哈希标记 (#) 分隔符的 txt 文件