R Lesson : Data Types in R | Dataplexa

Data Types in R

In this lesson, you will learn about data types in R. Data types define the kind of data a variable can store and how R treats that data.

Understanding data types is very important because all calculations, comparisons, and data analysis depend on them.


What Is a Data Type?

A data type tells R what kind of value is stored in a variable. For example, numbers, text, and logical values are all different data types.

R automatically detects the data type based on the value you assign. You do not need to declare it manually.


Numeric Data Type

Numeric data types are used to store numbers. They can include whole numbers and decimal values.

Numeric values are commonly used in calculations and statistical operations.

x <- 10
y <- 45.7
x
y

Both whole numbers and decimal numbers are treated as numeric in R.


Integer Data Type

Integers are whole numbers without decimal points. In R, integers are written using the letter L after the number.

This tells R explicitly that the value is an integer.

count <- 25L
count

Integers are useful when working with counts and indexes.


Character Data Type

Character data types store text values. They must always be written inside quotation marks.

Text can include letters, numbers, and symbols.

message <- "Welcome to R"
course <- "Dataplexa R Programming"

Without quotation marks, R will treat text as variable names and show errors.


Logical Data Type

Logical data types store Boolean values. They can only be TRUE or FALSE.

Logical values are widely used in conditions and decision-making.

is_active <- TRUE
is_completed <- FALSE

Logical values must always be written in uppercase.


Complex Data Type

R also supports complex numbers. These numbers include an imaginary part written with i.

Complex numbers are mainly used in advanced mathematical computations.

z <- 2 + 3i
z

Beginners may not use complex numbers often, but R supports them by default.


Checking the Data Type

You can check the data type of a variable using the class() function.

This function tells you how R interprets the stored value.

x <- 50
class(x)

name <- "Data Science"
class(name)

Knowing how to check data types helps debug programs easily.


Type Conversion (Type Casting)

Sometimes you may need to convert one data type into another. R provides functions for this purpose.

Common conversion functions include:

  • as.numeric()
  • as.character()
  • as.logical()
value <- "100"
new_value <- as.numeric(value)
class(new_value)

Type conversion is very common when working with real-world data.


Why Data Types Matter

Correct data types ensure accurate calculations and correct logic. Wrong data types can lead to errors or incorrect results.

Understanding data types helps you write efficient and reliable R programs.


📝 Practice Exercises


Exercise 1

Create a numeric variable and a character variable. Print both values and check their data types.

Exercise 2

Create a logical variable that stores whether a task is completed. Display its value.

Exercise 3

Store a number as text and convert it into numeric. Verify the data type before and after conversion.

Exercise 4

Create an integer variable and confirm that it is an integer data type.


✅ Practice Answers


Answer 1

num <- 45
text <- "R Programming"

class(num)
class(text)

Answer 2

completed <- TRUE
completed

Answer 3

value <- "250"
class(value)

converted_value <- as.numeric(value)
class(converted_value)

Answer 4

items <- 10L
class(items)

What’s Next?

Now that you understand data types, you are ready to work with collections of data.

In the next lesson, you will learn about vectors in R, which are one of the most important data structures in R programming.