Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define encoding in R script?

Tags:

Is it possible to define encoding in R script?

Something like that (just example):

encoding("utf-8")

I know for Reopen with encoding in RStudio, but I am not looking for this.

like image 840
Anette Avatar asked Jul 15 '16 11:07

Anette


People also ask

What encoding does r use?

Character strings in R can be declared to be encoded in "latin1" or "UTF-8" or as "bytes" . These declarations can be read by Encoding , which will return a character vector of values "latin1" , "UTF-8" "bytes" or "unknown" , or set, when value is recycled as needed and other values are silently treated as "unknown" .

How do I change the encoding of a file in R?

You can view or change this default in the Tools : Options (for Windows & Linux) or Preferences (for Mac) dialog, in the General section. If you don't set a default encoding, files will be opened using UTF-8 (on Mac desktop, Linux desktop, and server) or the system's default encoding (on Windows).


1 Answers

## x is intended to be in latin1
x <- "fa\xE7ile"
Encoding(x)
Encoding(x) <- "latin1"
x
xx <- iconv(x, "latin1", "UTF-8")
Encoding(c(x, xx))
c(x, xx)
Encoding(xx) <- "bytes"
xx # will be encoded in hex
cat("xx = ", xx, "\n", sep = "")

http://www.inside-r.org/r-doc/base/Encoding

https://stat.ethz.ch/R-manual/R-devel/library/base/html/Encoding.html

how to read data in utf-8 format in R?

like image 66
Hack-R Avatar answered Oct 06 '22 01:10

Hack-R