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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With