Conversion Functions in R Programming Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 6 Likes Like Report Sometimes to analyze data using R, we need to convert data into another data type. As we know R has the following data types Numeric, Integer, Logical, Character, etc. similarly R has various conversion functions that are used to convert the data type. In R, Conversion Function are of two types: Conversion Functions for Data Types Conversion Functions for Data Structures Conversion Functions For Data Types There are various conversion functions available for Data Types. These are: as.numeric() Decimal value known numeric values in R. It is the default data type for real numbers in R. In R as.numeric() converts any values into numeric values. Syntax: // Conversion into numeric data type as.numeric(x) Example: Python3 # A simple R program to convert # character data type into numeric data type x<-c('1', '2', '3') # Print x print(x) # Print the type of x print(typeof(x)) # Conversion into numeric data type y<-as.numeric(x) # print the type of y print(typeof(y)) Output: [1] "1" "2" "3" [1] "character" [1] "double" as.integer() In R, Integer data type is a collection of all integers. In order to create an integer variable in R and convert any data type in to Integer we use as.integer() function. Syntax: // Conversion of any data type into Integer data type as.integer(x) Example: Python3 # A simple R program to convert # numeric data type into integer data type x<-c(1.3, 5.6, 55.6) # Print x print(x) # Print type of x print(typeof(x)) # Conversion into integer data type y<-as.integer(x) # Print y print(y) # Print type of y print(typeof(y)) Output: [1] 1.3 5.6 55.6 [1] "double" [1] 1 5 55 [1] "integer" as.character() In R, character data is used to store character value and string. To create an character variable in R, we invoke the as.character() function and also if we want to convert any data type in to character we use as.character() function. Syntax: // Conversion of any data type into character data type as.character(x) Example: Python3 x<-c(1.3, 5.6, 55.6) # Print x print(x) # Print type of x print(typeof(x)) # Conversion into character data type y<-as.character(x) # Print y print(y) # Print type of y print(typeof(y)) Output: [1] 1.3 5.6 55.6 [1] "double" [1] "1.3" "5.6" "55.6" [1] "character" as.logical() Logical value is created to compare variables which return either true or false.To compare variables and to convert any value in to true or false, R uses as.logical() function. Syntax: // Conversion of any data type into logical data type as.logical(x) Example: Python3 x = 3 y = 8 # Conversion in to logical value result<-as.logical(x>y) # Print result print(result) Output: [1] FALSE as.date() In R as.date() function is used to convert string into date format. Syntax: // Print string into date format as.date(variable, "%m/%d/%y") Example: Python3 dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92") # Conversion into date format result<-as.Date(dates, "%m/%d/%y") # Print result print(result) Output: [1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01" Conversion Functions For Data Structure There are various conversion functions available for Data Structure. These are: as.data.frame() Data Frame is used to store data tables. Which is list of vectors of equal length. In R, sometimes to analyse data we need to convert list of vector into data.frame. So for this R uses as.data.frame() function to convert list of vector into data frame. Syntax: // Conversion of any data structure into data frame as.data.frame(x) Example: Python3 x<- list( c('a', 'b', 'c'), c('e', 'f', 'g'), c('h', 'i', 'j')) # Print x print(x) # Conversion in to data frame y<-as.data.frame(x) # Print y print(y) Output: [[1]] [1] "a" "b" "c" [[2]] [1] "e" "f" "g" [[3]] [1] "h" "i" "j" c..a....b....c.. c..e....f....g.. c..h....i....j.. 1 a e h 2 b f i 3 c g j as.vector() R has a function as.vector() which is used to convert a distributed matrix into a non-distributed vector. Vector generates a vector of the given length and mode. Syntax: // Conversion of any data structure into vector as.vector(x) Example: Python3 x<-c(a=1, b=2) # Print x print(x) # Conversion into vector y<-as.vector(x) # Print y print(y) Output: a b 1 2 [1] 1 2 as.matrix() In R, there is a function as.matrix() which is used to convert a data.table into a matrix, optionally using one of the columns in the data.table as the matrix row names. Syntax: // Conversion into matrix as.matrix(x) Example: Python3 # Importing library library(data.table) x <- data.table(A = letters[1:5], X = 1:5, Y = 6:10) # Print x print(x) # Conversion into matrix z<-as.matrix(x) # Print z print(z) Output: A X Y 1: a 1 6 2: b 2 7 3: c 3 8 4: d 4 9 5: e 5 10 A X Y [1,] "a" "1" " 6" [2,] "b" "2" " 7" [3,] "c" "3" " 8" [4,] "d" "4" " 9" [5,] "e" "5" "10" Create Quiz Comment P priyanka_bajpai Follow 6 Improve P priyanka_bajpai Follow 6 Improve Article Tags : Articles R Language R-dataStructures R Functions Explore IntroductionR Programming Language - Introduction 4 min read Interesting Facts about R Programming Language 4 min read R vs Python 5 min read Environments in R Programming 3 min read Introduction to R Studio 4 min read How to Install R and R Studio? 4 min read Creation and Execution of R File in R Studio 5 min read Clear the Console and the Environment in R Studio 2 min read Hello World in R Programming 2 min read Fundamentals of RBasic Syntax in R Programming 3 min read Comments in R 3 min read R-Operators 5 min read R-Keywords 2 min read R-Data Types 5 min read VariablesR Variables - Creating, Naming and Using Variables in R 5 min read Scope of Variable in R 5 min read Dynamic Scoping in R Programming 5 min read Lexical Scoping in R Programming 4 min read Input/OutputTaking Input from User in R Programming 7 min read Printing Output of an R Program 4 min read Print the Argument to the Screen in R Programming - print() Function 2 min read Control FlowControl Statements in R Programming 4 min read Decision Making in R Programming - if, if-else, if-else-if ladder, nested if-else, and switch 3 min read Switch case in R 2 min read For loop in R 5 min read R - while loop 5 min read R - Repeat loop 2 min read goto statement in R Programming 2 min read Break and Next statements in R 3 min read FunctionsFunctions in R Programming 5 min read Function Arguments in R Programming 4 min read Types of Functions in R Programming 6 min read Recursive Functions in R Programming 4 min read Conversion Functions in R Programming 4 min read Data StructuresData Structures in R Programming 4 min read R Strings 6 min read R-Vectors 4 min read R-Lists 6 min read R - Array 7 min read R-Matrices 10 min read R-Factors 4 min read R-Data Frames 6 min read Object Oriented ProgrammingR-Object Oriented Programming 7 min read Classes in R Programming 3 min read R-Objects 3 min read Encapsulation in R Programming 3 min read Polymorphism in R Programming 6 min read R - Inheritance 7 min read Abstraction in R Programming 3 min read Looping over Objects in R Programming 5 min read S3 class in R Programming 8 min read Explicit Coercion in R Programming 3 min read Error HandlingHandling Errors in R Programming 3 min read Condition Handling in R Programming 5 min read Debugging in R Programming 3 min read File HandlingFile Handling in R Programming 3 min read Reading Files in R Programming 9 min read Writing to Files in R Programming 2 min read Working with Binary Files in R Programming 5 min read Like