r - 列名从 CSV 更改为 R : dot instead of slash

标签 r csv

当我在 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/

相关文章:

r - 使数据框中的所有元素都是唯一的

r - 如何可视化 tm 的 findAssocs() 结果

r - 如何将 lapply 与传递给 2-way tabyl 的列列表一起使用

Python 3 CSV 文件给出 UnicodeDecodeError : 'utf-8' codec can't decode byte error when I print

java - Hibernate在保存大量实体时导致内存不足异常

vba - 运行循环以根据标题名称检查列,并在缺少列时插入列

python - 使用 python 3 将 CSV 转换为 JSON

html - 使用 rvest 跟随 "next"与相对路径的链接

r - 将 xkcd 包与 ggplot2 一起使用 : How do I color a bar graph but keep the man solid?

python - 使用Python编辑电子表格(通过CSV文件传递)