问题
I work with windows 7 (my system: "LC_COLLATE=French_France.1252) with data with accents.
My data are coded in ANSI which allows me to visualize them correctly in the tabs of Rstudio.
My problem: When I want to a create GoogleVis page (encoding utf-8), the accented characters are not displayed correctly.
What I expected: I am looking to convert my latin1 Data.frames in utf-8 with R just before creating googleVis pages. I have no ideas. Stringi package seems only to work with raw data.
fr <- data.frame(âge = c(15,20), prénom = c("Adélia", "Adão"), row.names = c("I1", "I2"))
print (fr)
library (googleVis)
test <- gvisTable(fr)
plot(fr)
the real data https://drive.google.com/open?id=0B91cr4hfMXV4OEkzWk1aWlhvR0E
# importing (historical data)
test_ansi<-read.table("databig_ansi.csv",
header=TRUE, sep=",",
na.strings="",
quote = "\"",
dec=".")
# subsetting
library (dplyr)
test_ansi <-
test_ansi %>%
count(ownera)
# library (stringi)
stri_enc_detect(test_ansi$ownera)
# visualisation
library (googleVis)
testvis <- gvisTable(test_ansi)
plot(testvis)
回答1:
There are built-in functions in several packages, such as stringi
, stringr
, SoundexBR
, tau
, as well as a character convert in the R base system, which can be used as:
text2 <- iconv(text, from = "latin1", to = "UTF-8")
You may also want a more specific function with some checks for factors, like the following:
.fromto <- function (x, from, to)
{
if (is.list(x)) {
xattr <- attributes(x)
x <- lapply(x, .fromto, from, to)
attributes(x) <- xattr
} else {
if (is.factor(x)) {
levels(x) <- iconv(levels(x), from, to, sub = "byte")
} else {
if (is.character(x))
x <- iconv(x, from, to, sub = "byte")
}
lb <- attr(x, "label")
if (length(lb) > 0) {
attr(x, "label") <- iconv(attr(x, "label"), from, to, sub = "byte")
}
}
x
}
# This will convert a vector from any encoding into UTF-8
Latin2UTF8 <- function (x, from = "WINDOWS-1252")
{
.fromto(x, from, "UTF-8")
}
Then you just use it as:
Latin2UTF8(fr)
âge prénom
I1 15 Adélia
I2 20 Adão
Extra edits after extra information and data
This is how my R is setup. My R is running on UTF-8 locale and English by default. Once my system environment differs from the file encoding provided, I'll use fileEncoding = "LATIN1"
. That is all.
> Sys.getlocale()
[1] "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"
test_ansi<-read.table(file.choose(),
header=TRUE, sep=",",
na.strings="",
quote = "\"",
dec=".", fileEncoding = "LATIN1")
> test_ansi2 <-
+ test_ansi %>%
+ count(ownera)
> test_ansi2
Source: local data frame [6,482 x 2]
ownera n
1 Abautret (Vve) 1
2 Abazuza 1
3 Abernathy 1
4 Abrahamsen, Heerman 1
5 Abrahamsen, Hereman 6
6 Abrahamsz, Heerman 2
7 Abram, Ralph 8
8 Abrams, Heerman 2
9 Abranches 1
10 Abreu 1
.. ... .
# visualisation
library (googleVis)
testvis <- gvisTable(test_ansi)
plot(testvis)
Link to the table created
来源:https://stackoverflow.com/questions/34496328/recoding-data-fame-object-from-latin1-to-utf-8