You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过R语言read.delim()读取txt文件时保留复杂列名原符号?

Keep Original Column Names with read.delim()

Absolutely! You don’t have to convert your .txt file to Excel to retain those special characters in your column headers—read.delim() has a simple built-in fix for this.

By default, read.delim() (which inherits behavior from read.table()) uses check.names = TRUE, which automatically replaces any characters that aren’t letters, numbers, or dots with dots. This is why your spaces, +, -, and other symbols get converted.

To preserve the original column names exactly as they appear in your .txt file, just set check.names = FALSE when calling the function:

# Read the text file with all special characters in column names preserved
your_data <- read.delim("your_file_path.txt", check.names = FALSE)

This will keep every character in your column headers intact—spaces, /, +, -, |, and all. The result will match what you get when using readxl::read_excel() on an Excel version of the file.

A quick note for working with these column names later: since they contain special characters, you’ll need to wrap them in backticks if you reference them directly with the $ operator, or use double brackets:

# Example of accessing the tricky column name
your_data$`Basket/Fruit/Many Apples/Pears+ Cherries-/Seeds+ Peel-| Freq. (%)`
# Or using double brackets
your_data[["Basket/Fruit/Many Apples/Pears+ Cherries-/Seeds+ Peel-| Freq. (%)"]]

内容的提问来源于stack exchange,提问作者lostnewbiecoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 10:16:33