[ 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)

#  raw: Raw Vectors

##  Description

Creates or tests for objects of type ` "raw" ` .

##  Usage

    
    
    raw(length = 0)
    as.raw(x)
    is.raw(x)

##  Arguments

length

desired length.

x

object to be coerced.

##  Value

` raw ` creates a raw vector of the specified length. Each element of the
vector is equal to ` 0 ` . Raw vectors are used to store fixed-length
sequences of bytes.

` as.raw ` attempts to coerce its argument to be of raw type. The
(elementwise) answer will be ` 0 ` unless the coercion succeeds (or if the
original value successfully coerces to 0).

` is.raw ` returns true if and only if ` typeof(x) == "raw" ` .

##  Details

The raw type is intended to hold raw bytes. It is possible to extract
subsequences of bytes, and to replace elements (but only by elements of a raw
vector). The relational operators (see [ Comparison
](/link/Comparison?package=base&version=3.6.2) , using the numerical order of
the byte representation) work, as do the logical operators (see [ Logic
](/link/Logic?package=base&version=3.6.2) ) with a bitwise interpretation.

A raw vector is printed with each byte separately represented as a pair of hex
digits. If you want to see a character representation (with escape sequences
for non-printing characters) use ` [ rawToChar
](/link/rawToChar?package=base&version=3.6.2) ` .

Coercion to raw treats the input values as representing small (decimal)
integers, so the input is first coerced to integer, and then values which are
outside the range ` [0 … 255] ` or are ` NA ` are set to ` 0 ` (the ` nul `
byte).

` as.raw ` and ` is.raw ` are [ primitive
](/link/primitive?package=base&version=3.6.2) functions.

##  See Also

` [ charToRaw ](/link/charToRaw?package=base&version=3.6.2) ` , ` [ rawShift
](/link/rawShift?package=base&version=3.6.2) ` , etc.

` [ & ](/link/%26amp%3B?package=base&version=3.6.2) ` for bitwise operations
on raw vectors.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fraw&utm_source=r-docs&utm_medium=docs&utm_term=raw&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    xx <- raw(2)
    xx[1] <- as.raw(40)     # NB, not just 40.
    xx[2] <- charToRaw("A")
    xx       ## 28 41   -- raw prints hexadecimals
    dput(xx) ## as.raw(c(0x28, 0x41))
    as.integer(xx) ## 40 65
    
    x <- "A test string"
    (y <- charToRaw(x))
    is.vector(y) # TRUE
    rawToChar(y)
    is.raw(x)
    is.raw(y)
    stopifnot( charToRaw("\xa3") == as.raw(0xa3) )
    
    isASCII <-  function(txt) all(charToRaw(txt) <= as.raw(127))
    isASCII(x)  # true
    isASCII("\xa325.63") # false (in Latin-1, this is an amount in UK pounds)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fraw&utm_source=r-docs&utm_medium=docs&utm_term=raw&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)