Data Types in R

Interactive beginner R tutorial on data types: numeric, character, logical and factor β€” checking types with class() and is.(), and converting with as.(). Run real R in your browser.

Every value in R has a type, and the type decides what you can do with it. Everything below is a live R console β€” edit the code and press Run Code.

Watch: data types in R

The four types you meet first β€” numeric, character, logical and factor β€” plus how to check and convert between them.

Download the R script  Β·  β–Ά Practice in the playground β€” or edit and run each snippet below.

The four core types

class() tells you what kind of value you have:

Check a type, or convert it

is.*() asks a yes/no question about a type; as.*() converts from one to another:

Logicals are numbers underneath

TRUE counts as 1 and FALSE as 0, so summing a logical vector counts the TRUEs and the mean gives a proportion β€” a trick you’ll use constantly:

Factors: categorical data

A factor stores categories with a fixed set of levels, kept internally as codes β€” the right type for groups:

Your turn


Next: Subsetting & indexing β†’ β€” pulling out exactly the values you want.