【问题标题】:Column name from txt/csv imported as character in R来自 txt/csv 的列名在 R 中作为字符导入
【发布时间】:2023-01-18 02:12:15
【问题描述】:

我使用 .txt 文件,这些文件是用复杂的列名生成的,例如 Basket/Fruit/Many Apples/Pears+ Cherries-/Seeds+ Peel-| Freq. (%)

但是,read.delim() 会替换所有空格,+- 将替换为点 .。我需要这些符号来理解我的数据。

如果我将我的文件转换为.xlsx并通读readxl::read_excel(),我得到的列名完整为character

我可以直接通过我原来的.txt 文件和read.delim() 以某种方式实现这一点,而不用替换列名称中的符号吗?

【问题讨论】:

标签: r character txt columnname


【解决方案1】:

如果这是您的文本文件

% cat file.txt
Basket/Fruit/Many Apples/Pears+ Cherries-/Seeds+ Peel-| Freq. (%)
1 2 3 4 5 6
2 3 4 5 6 7

阅读例如read.delim

read.delim('file.txt', header=T, check.names=F, sep=" ")

输出

  Basket/Fruit/Many Apples/Pears+ Cherries-/Seeds+ Peel-| Freq. (%)
1                 1             2                3      4     5   6
2                 2             3                4      5     6   7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2015-04-14
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2019-04-16
    • 2021-09-01
    相关资源
    最近更新 更多