当我在 R 中导入 CSV 文件时,列的名称会发生变化。它们从“Fe/Cu”变为“Fe.Cu”。但我只在一个 CSV 文件中遇到这个神秘的问题。 我尝试重命名列
colnames(a[12:ncol(a)])=c("Fe/Cu","Fe/Zn","Fe/Ba")
但是什么也没发生。有什么想法吗?
最佳答案
将 check.names=FALSE
添加到 csv.read
函数调用将允许您获取原始名称。这是因为默认情况下 csv.read
将检查列名称在语法上是否有效,如注释中 @Asayat 所述。
来自 csv.read
文档:
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.
如果您随后检查 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.
这就是 @Asayat 的评论。
关于r - 列名从 CSV 更改为 R : dot instead of slash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999260/