Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read csv file in r with spanish characters (´,ñ)

Tags:

r

encoding

utf-8

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.

like image 554
Juliana Gómez Avatar asked Jul 17 '15 20:07

Juliana Gómez


2 Answers

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)

like image 173
Dante Avatar answered Nov 17 '22 20:11

Dante


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
like image 23
tumultous_rooster Avatar answered Nov 17 '22 20:11

tumultous_rooster