9

I´m trying to add a column to a data frame in R. To do this, I imported a CSV file from Excel, which contains the id column (the same as the one I have in the data frame), and the column with the information I want to add to my data frame.

My problem is that my cvs has spanish characters (´, ñ), and when I use read.csv (as in the following example)

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

the characters don't appear, but a question mark appears instead of the characters.

I have tried changing the encoding, with the following encodings:

UTF-8, latin1,

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

But there is no difference.

I gladly appreciate any help.

rawr
  • 20,481
  • 4
  • 44
  • 78
Juliana Gómez
  • 301
  • 1
  • 3
  • 10

2 Answers2

11

Use the encoding option inside your read.csv code

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

Remember, you can always check for documentation in R using help(function)

Dante
  • 154
  • 1
  • 8
1

You could expand off something like this:

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
tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149