Data Types in R

R Tutorial – We shall learn about R atomic data types, common R object data structures, their syntax and example R commands for checking and converting data types.

In R, every value you work with is stored inside an object. A variable name points to an R object, and that object has a type. The type tells R how the value should be stored, compared, printed and used in calculations.

Unlike statically typed languages, R does not require you to declare the data type of a variable before assigning a value. R derives the type from the object assigned to the variable. For example, x <- TRUE creates a logical object, while x <- "TRUE" creates a character object.

R has six main atomic data types: logical, numeric, integer, complex, character and raw. Common R data structures such as vectors, matrices, arrays, factors, lists and data frames are built using these basic types.

R atomic data types and the values they store

Atomic data types are the simplest data types in R. An atomic vector can hold one or more values, but all values in that atomic vector must be of the same basic type.

Data TypeExampleDescription
LogicalTRUE, FALSEBoolean values used for conditions and filtering.
Numeric2, 45.9, 3782Numbers with decimal support. In R, ordinary numbers are usually stored as double precision numeric values.
Integer9L, 779LWhole numbers explicitly marked with the suffix L.
Complex8+9iNumbers with real and imaginary parts.
Character‘m’, “hello”Text values, including single characters and strings.
Raw [68, 65, 6C, 6C,6F] is the value for string hello.Raw bytes, usually used when handling binary data or low-level input/output.

Note : When data type is Raw, user has to know the format or protocol of the data.

class(), typeof() and str() for checking R data types

R provides more than one way to inspect data. The most commonly used functions are class(), typeof() and str(). They answer slightly different questions.

FunctionWhat it tells youExample use
class()The user-facing class of the object.class(10) returns "numeric".
typeof()The internal storage type used by R.typeof(10) returns "double".
str()A compact structure summary of an object.Useful for lists and data frames.
</>
Copy
class(object)
typeof(object)
str(object)

The difference between class() and typeof() is important for beginners. A number such as 67.54 has class "numeric", but its internal type is usually "double". An explicitly marked whole number such as 63L has class and internal type "integer".

</>
Copy
x <- 67.54
class(x)
typeof(x)

y <- 63L
class(y)
typeof(y)
[1] "numeric"
[1] "double"
[1] "integer"
[1] "integer"

Examples of R atomic vectors by data type

We shall run the following commands to assign variables, data of different data types and print the class of the variable to verify the data type.

Logical data type in R

The logical data type stores TRUE or FALSE. Logical values are commonly returned by comparison operators such as >, == and !=.

</>
Copy
> x <- TRUE
> print(class(x))
[1] "logical"

Numeric data type in R

The numeric data type is used for ordinary numbers. Unless you explicitly mark a whole number as an integer with L, R generally treats it as numeric.

</>
Copy
> x <- 67.54
> print(class(x))
[1] "numeric"

Integer data type in R

The integer data type is used for whole numbers that are explicitly written with the suffix L. For example, 63 is numeric, but 63L is integer.

</>
Copy
x <- 63L
> print(class(x))
[1] "integer"

Complex data type in R

The complex data type stores values with real and imaginary parts. The imaginary part is written with i.

</>
Copy
> x <- 6 + 4i
> print(class(x))
[1] "complex"

Character data type in R

The character data type stores text. Both single quotes and double quotes can be used to create character values in R.

</>
Copy
> x <- "hello"
> print(class(x))
[1] "character"

Raw data type in R

The raw data type stores bytes. It is less common in beginner data analysis, but it is useful when working with binary files, encoded text or byte-level operations.

</>
Copy
> x <- charToRaw("hello")
> print(class(x))
[1] "raw"

R object data types and data structures

As already mentioned there are many types of R Objects. We shall look into some of the most commonly used data structures in R. They are :

  • Vectors
  • Lists
  • Matrices
  • Arrays
  • Factors
  • Data Frames

These structures are not all data types in the same low-level sense. They are R objects that organize values. Some are homogeneous, meaning all elements must have the same atomic type. Others can store mixed types.

R objectCan it hold mixed data types?Typical use
VectorNo, atomic vectors are homogeneous.A sequence of values of the same type.
MatrixNo, all matrix elements share one atomic type.Two-dimensional numeric or character data.
ArrayNo, all array elements share one atomic type.Multi-dimensional data.
FactorStores categorical values as levels.Groups such as gender, grade, region or status.
ListYes.Mixed objects such as numbers, strings, functions and data frames.
Data FrameYes, across columns. Each column is a vector.Tabular data with rows and columns.

R vectors store one atomic data type at a time

In R programming language, a Vector is a fixed-length collection of values of a data type. The vector would get the data type of items in the collection.

Syntax – Define a Vector

</>
Copy
variable <- c(comma separated atomic vectors belonging to a data type)

For example (‘apple’,’orange’,”banana”) is a vector and is a collection of values of data type Character. So the vector would become a Character Vector. Similarly an Integer Vector or Complex Vector.

Following is an example of a Character Vector. We shall learn how to assign an R Character Vector to a variable, print the vector and verify the data type of vector.

</>
Copy
> fruits = c('apple','orange',"banana") > print(class(fruits))
[1] "character"
> print(fruits)
[1] "apple"  "orange" "banana"

When values of different atomic types are combined in one atomic vector, R coerces them to one common type. This is why a vector containing both numbers and text becomes a character vector.

</>
Copy
mixed_values <- c(10, TRUE, "apple")
mixed_values
class(mixed_values)
[1] "10"    "TRUE"  "apple"
[1] "character"

This automatic conversion is called type coercion. It is useful, but it can also cause mistakes if you expect a numeric vector and one value is accidentally entered as text.

R lists store mixed objects without coercing every value

In R programming language, a List is a collection of List Items (R Objects) belonging to different data types. A List may contain another list as its item. A List Item may contain a Matrices, an Array, a Factor, an R function or any of R Object.

Syntax to Define List

</>
Copy
variable <- list(comma seperated list items)

Following is an example of an R List. We shall learn how to assign a list of Number, Character, Function and another list  to a List and print the List.

</>
Copy
> listX = list(51,"hello",tan,list(8L,"a")) > print(listX)
[[1]]
[1] 51

[[2]]
[1] "hello"

[[3]]
function (x)  .Primitive("tan")

[[4]]
[[4]][[1]]
[1] 8

[[4]][[2]]
[1] "a"

Please observe that fourth and last item in the list is another list.

Use a list when the items do not have the same shape or type. For example, a list can store a numeric vector, a character label, a logical flag and a function in the same object.

R matrices store two-dimensional data of one type

In R programming language, A Matrix is a 2-D set of data elements. A Vector, number of rows and number of columns could be used to create a Matrix.

Syntax – Define Matrix

variable <- matrix(vector, number of rows, number of columns, split by row or column)

split by row or column : if TRUE then its split by row, else if its FALSE then split by column.

Following is an example to define a matrix :

1. Split by row.

</>
Copy
> A = matrix(c(1,2,3,4,5,6,7,8),2,4,TRUE) > print(A)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8

2. Split by column.

</>
Copy
> A <- matrix(c(1,2,3,4,5,6,7,8),2,4,FALSE) > print(A)
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8

A matrix is homogeneous. If you mix numbers and text in a matrix, R converts the values to one common type, often character.

R arrays store multi-dimensional data of one type

In R programming language, Arrays are N-Dimensional data sets.

Syntax – Define an R Array

</>
Copy
variable <- array(list, dimension)

where list contains the elements of array and dimension is a list containing the information about dimensionality of the array. If dimension is c(2,5,4,8), the array is 4-Dimensional with dimensions 2x5x4x8.

Following is an example of 3-D array.

</>
Copy
> A = array(c(1,2,3,4,5,6,7,8,9,10,11,12),c(2,3,2)) > print(A)
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

An array is useful when a matrix is not enough. For example, a 3-D array can store values by row, column and layer.

R factors store categorical data with levels

In R programming language, a Factor is a vector along with the distinct values of vector as levels. Factors are useful during statistical modelling.

Levels are stored as R Characters.

Syntax – Define an R Factor

</>
Copy
variable <- factor(vector)

Following is an example to define an R Factor

</>
Copy
> factorX = factor(c(1,4,7,2,6,7,1,6,4)) > print(factorX)
[1] 1 4 7 2 6 7 1 6 4
Levels: 1 2 4 6 7

Use factors for categorical variables, especially when the set of allowed categories matters. For example, survey answers such as "low", "medium" and "high" are often clearer as factors than as plain character strings.

</>
Copy
priority <- factor(c("low", "high", "medium", "low"),
                   levels = c("low", "medium", "high"),
                   ordered = TRUE)

priority
[1] low    high   medium low   
Levels: low < medium < high

R data frames store tabular data with typed columns

In R programming language, a Data Frame is a set of equal length vectors. The vectors could be of different data types.

Syntax – Define an R Data Frame

</>
Copy
variable <- data.frame(listA, listB, listC, .., listN)

Following is an example to define an R Data Frame :

</>
Copy
> dataX = data.frame(values = c(21,42,113), RGB = c('red','blue','green')) > print(dataX)
  values   RGB
1     21   red
2     42  blue
3    113 green

A data frame is the most common structure for data analysis in R. Each column is a vector, so every column has its own type, and all columns must have the same length.

</>
Copy
students <- data.frame(
  roll_no = c(1L, 2L, 3L),
  name = c("Anu", "Bala", "Chitra"),
  passed = c(TRUE, FALSE, TRUE)
)

str(students)
'data.frame':	3 obs. of  3 variables:
 $ roll_no: int  1 2 3
 $ name   : chr  "Anu" "Bala" "Chitra"
 $ passed : logi  TRUE FALSE TRUE

Type conversion and coercion in R data types

R can convert values from one type to another. Some conversions happen automatically, while others should be done explicitly with conversion functions. Explicit conversion is usually safer because the code clearly shows what type you expect.

Conversion functionPurpose
as.numeric()Converts a value to numeric, if possible.
as.integer()Converts a value to integer. Decimal part is truncated.
as.character()Converts a value to text.
as.logical()Converts compatible values to TRUE or FALSE.
as.factor()Converts a vector to a factor.
</>
Copy
marks_text <- c("80", "75", "92")
marks_numeric <- as.numeric(marks_text)

as.integer(67.9)
as.character(TRUE)
marks_numeric
[1] 67
[1] "TRUE"
[1] 80 75 92

If a character value cannot be converted to a number, R returns NA and usually gives a warning. This is common when imported data contains symbols, spaces or text in a numeric column.

</>
Copy
as.numeric(c("10", "20", "not available"))
[1] 10 20 NA
Warning message:
NAs introduced by coercion

Special R values: NA, NULL, NaN and Inf

While learning R data types, it is also important to understand a few special values. They are not ordinary categories like numeric or character, but they appear often in real data and calculations.

Special valueMeaningExample
NAMissing value.A blank value in an imported data column.
NULLNo object or empty result.A function may return NULL when no value is available.
NaNNot a Number.0/0
Inf and -InfPositive and negative infinity.1/0 and -1/0
</>
Copy
is.na(NA)
is.null(NULL)
0 / 0
1 / 0
[1] TRUE
[1] TRUE
[1] NaN
[1] Inf

Use is.na() to test missing values. Do not compare missing values using == NA, because missingness is not the same as equality.

Choosing the correct R data type for common data

The right R data type depends on what the value represents, not only on how it looks. A value such as "001" may look numeric, but if it is an ID code, it should usually remain character so that leading zeros are preserved.

Data you haveRecommended R type or structureReason
Marks, prices, measurementsNumericThese values are used in arithmetic.
Counts that must be whole numbersInteger or numericUse integer when exact whole-number storage matters.
Names, IDs, codesCharacterThese values should not be treated as quantities.
Yes/no or pass/fail valuesLogical or factorUse logical for TRUE/FALSE logic; use factor for labelled categories.
Low/medium/high categoriesOrdered factorThe category order has meaning.
Spreadsheet-like rows and columnsData frameDifferent columns can have different types.
Mixed values in one objectListLists can hold different types without forcing one common type.

Common mistakes with R data types

  • Expecting ordinary whole numbers to be integers: In R, 10 is usually numeric/double. Use 10L for an integer.
  • Mixing numbers and text in one vector: R coerces the vector to a common type, often character.
  • Using character data for calculations: Imported numbers may arrive as text. Check with str() before calculating.
  • Using factors when plain text is needed: Factors are useful for categories, but character vectors are simpler for free-form text.
  • Testing missing values with equality: Use is.na(x), not x == NA.

Frequently asked questions about R data types

What are the six atomic data types in R?

The six atomic data types in R are logical, numeric, integer, complex, character and raw. These are the basic types used to build atomic vectors and many higher-level R objects.

What is the difference between class() and typeof() in R?

class() tells you the object class used for printing and method dispatch, while typeof() tells you the internal storage type. For example, class(10) returns "numeric", but typeof(10) returns "double".

Why is 10 numeric and not integer in R?

R treats ordinary numbers such as 10 as numeric values. To create an integer explicitly, add the suffix L, as in 10L.

Can one R vector contain different data types?

An atomic vector cannot store different data types as separate types. If you combine mixed values, R coerces them to one common type. Use a list when you need to store mixed data types in one object.

When should I use a factor instead of a character vector in R?

Use a factor when values represent categories with a known set of levels, such as "low", "medium" and "high". Use a character vector for ordinary text, names, descriptions or values that do not have fixed categories.

Editorial QA checklist for this R data types tutorial

  • The tutorial clearly separates R atomic data types from R object data structures.
  • The examples show both beginner-friendly class() checks and the important typeof() distinction.
  • The explanation covers vector coercion, because it is a common source of errors in R programs.
  • The data frame section explains that each column is a vector with its own type.
  • The FAQ answers topic-specific questions beginners ask about numeric, integer, factor and mixed-type values in R.

Summary of R data types for beginners

In this R Tutorial, we have learnt about different R atomic data types and different data types of R-Objects used most commonly in R programming language. The main point to remember is that atomic vectors store one basic type, while lists and data frames are used when you need to organize different types of values together.