【问题标题】:read csv file in r with spanish characters (´,ñ)用西班牙语字符(´,ñ)在r中读取csv文件
【发布时间】:2015-10-07 15:43:44
【问题描述】:

我正在尝试在 R 中的数据框中添加一列。为此,我从 Excel 导入了一个 CSV 文件,其中包含 id 列(与我在数据框中的列相同),并且包含我要添加到数据框中的信息的列。

我的问题是我的 cvs 有西班牙语字符(´、ñ),当我使用 read.csv 时(如下例所示)

religion <- read.csv("religion.csv", header = TRUE, sep = ",", dec = ".",
                     filled =TRUE, comment.char = "", strip.white = TRUE,
                     stringsAsFactors = TRUE)

字符没有出现,但出现的是问号而不是字符。

我已尝试更改编码,使用以下编码:

UTF-8, latin1,

Sys.setlocale("LC_ALL", "ES_ES.UTF-8")

但是没有区别。

非常感谢任何帮助。

【问题讨论】:

  • 文件中使用的编码是什么?您使用的是什么操作系统? reproducible example 将有助于了解发生了什么。
  • 这个工作dt&lt;-data.table(a="ñ")吗?
  • filled 不是 read.csv 的参数,你的意思是fill

标签: r encoding utf-8


【解决方案1】:

你可以这样展开:

DF<- data.frame(col1=c(1,2), col2=c("there is an ñ here", "there is an ´ here"))
#   col1    col2
#    1      there is an ñ here
#    2      there is an ´ here

DF$col2 <- chartr("ñ", "n", DF$col2)
DF$col2 <- chartr("´", "'", DF$col2)
DF
#  col1     col2
#   1       there is an n here
#   2       there is an ' here

【讨论】:

    【解决方案2】:

    read.csv 代码中使用encoding 选项

        religion <- read.csv("religion.csv", header = TRUE, sep = ",", dec = ".",
                             filled =TRUE, comment.char = "", strip.white = TRUE,
                             stringsAsFactors = TRUE, encoding="UTF-8")
    

    请记住,您始终可以使用 help(function) 在 R 中检查文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 2014-12-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多