Converting Types of Data

“Errors using inadequate data are much less than those using no data at all.” – Charles Babbage

In R, vectors are automatically assigned to a certain data type, or the data type of a vector can be subsequently converted into another data type. The query, which data type it is, is made with a preceding is. (e.g. is.numeric()). With a preceding as. one can change the data type of a vector (e.g. as.numeric()).

There are multiple classes that are grouped together as “numeric” classes. The two most common of which are double (for double precision floating point numbers) and integer. R will automatically convert between the numeric classes when needed, so for the most part it does not matter to the casual user whether the number 3 is currently stored as an integer or as a double. Most math is done using double precision, so that is often the default storage.

Sometimes you may want to specifically store a vector as integers if you know that they will never be converted to doubles (used as ID values or indexing) since integers require less storage space. But if they are going to be used in any math that will convert them to double, then it will probably be quickest to just store them as doubles to begin with.

The table below gives an overview of the different data types.

Data Type is.function as.function
numeric is.numeric() as.numeric()
integer is.integer() as.integer()
double is.double() as.double()
character is.character() as.character()
factor is.factor() as.factor()
logical is.logical() as.logical()
NULL is.null() as.null()

The generic function indicates which elements are missing.

The generic function <- sets elements to NA.


value <- 23.5

[1] TRUE

color <- c("blue","red","red","yellow")
[1] "blue"   "red"    "red"    "yellow"

[1] TRUE

color.factor <- as.factor(color)
[1] blue   red    red    yellow
Levels: blue red yellow

Putting capital ‘L’ after an integer forces it to be stored as an integer.

[1] TRUE


[1] TRUE

[1] TRUE