[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  Encoding: Read or Set the Declared Encodings for a Character Vector

##  Description

Read or set the declared encodings for a character vector.

##  Usage

    
    
    Encoding(x)
    
    Encoding(x) <- value
    
    enc2native(x)
    enc2utf8(x)

##  Arguments

x

A character vector.

value

A character vector of positive length.

##  Value

A character vector.

For ` enc2utf8 ` encodings are always marked: they are for ` enc2native ` in
UTF-8 and Latin-1 locales.

##  Details

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" ` . ASCII strings will never
be marked with a declared encoding, since their representation is the same in
all supported encodings. Strings marked as ` "bytes" ` are intended to be non-
ASCII strings which should be manipulated as bytes, and never converted to a
character encoding (so writing them to a text file is supported only by `
writeLines(useBytes = TRUE) ` ).

` enc2native ` and ` enc2utf8 ` convert elements of character vectors to the
native encoding or UTF-8 respectively, taking any marked encoding into
account. They are [ primitive ](/link/primitive?package=base&version=3.6.2)
functions, designed to do minimal copying.

There are other ways for character strings to acquire a declared encoding
apart from explicitly setting it (and these have changed as  R  has evolved).
Functions ` [ scan ](/link/scan?package=base&version=3.6.2) ` , ` [ read.table
](/link/read.table?package=base&version=3.6.2) ` , ` [ readLines
](/link/readLines?package=base&version=3.6.2) ` , and ` [ parse
](/link/parse?package=base&version=3.6.2) ` have an ` encoding ` argument that
is used to declare encodings, ` [ iconv
](/link/iconv?package=base&version=3.6.2) ` declares encodings from its ` to `
argument, and console input in suitable locales is also declared. ` [
intToUtf8 ](/link/intToUtf8?package=base&version=3.6.2) ` declares its output
as ` "UTF-8" ` , and output text connections (see ` [ textConnection
](/link/textConnection?package=base&version=3.6.2) ` ) are marked if running
in a suitable locale. Under some circumstances (see its help page) ` [ source
](/link/source?package=base&version=3.6.2) (encoding=) ` will mark encodings
of character strings it outputs.

Most character manipulation functions will set the encoding on output strings
if it was declared on the corresponding input. These include ` [ chartr
](/link/chartr?package=base&version=3.6.2) ` , ` [ strsplit
](/link/strsplit?package=base&version=3.6.2) (useBytes = FALSE) ` , ` [
tolower ](/link/tolower?package=base&version=3.6.2) ` and ` [ toupper
](/link/toupper?package=base&version=3.6.2) ` as well as ` [ sub
](/link/sub?package=base&version=3.6.2) (useBytes = FALSE) ` and ` [ gsub
](/link/gsub?package=base&version=3.6.2) (useBytes = FALSE) ` . Note that such
functions do not _preserve_ the encoding, but if they know the input encoding
and that the string has been successfully re-encoded (to the current encoding
or UTF-8), they mark the output.

` [ substr ](/link/substr?package=base&version=3.6.2) ` does preserve the
encoding, and ` [ chartr ](/link/chartr?package=base&version=3.6.2) ` , ` [
tolower ](/link/tolower?package=base&version=3.6.2) ` and ` [ toupper
](/link/toupper?package=base&version=3.6.2) ` preserve UTF-8 encoding on
systems with Unicode wide characters. With their ` fixed ` and ` perl `
options, ` [ strsplit ](/link/strsplit?package=base&version=3.6.2) ` , ` [ sub
](/link/sub?package=base&version=3.6.2) `