Conversion Functions in R Programming
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:
# A simple R program to convert# character data type into numeric data typex<-c('1','2','3')# Print xprint(x)# Print the type of xprint(typeof(x))# Conversion into numeric data typey<-as.numeric(x)# print the type of yprint(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:
# A simple R program to convert# numeric data type into integer data typex<-c(1.3,5.6,55.6)# Print xprint(x)# Print type of xprint(typeof(x))# Conversion into integer data typey<-as.integer(x)# Print yprint(y)# Print type of yprint(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:
x<-c(1.3,5.6,55.6)# Print xprint(x)# Print type of xprint(typeof(x))# Conversion into character data typey<-as.character(x)# Print yprint(y)# Print type of yprint(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:
x=3y=8# Conversion in to logical valueresult<-as.logical(x>y)# Print resultprint(result)Output:
[1] FALSE
- as.date()
In Ras.date()function is used to convert string into date format.Syntax:
// Print string into date format as.date(variable, "%m/%d/%y")
Example:
dates <-c("02/27/92","02/27/92","01/14/92","02/28/92","02/01/92")# Conversion into date formatresult<-as.Date(dates,"%m/%d/%y")# Print resultprint(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:
x<-list( c('a','b','c'),c('e','f','g'), c('h','i','j'))# Print xprint(x)# Conversion in to data framey<-as.data.frame(x)# Print yprint(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:
x<-c(a=1, b=2)# Print xprint(x)# Conversion into vectory<-as.vector(x)# Print yprint(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:
# Importing librarylibrary(data.table)x <-data.table(A=letters[1:5], X=1:5, Y=6:10)# Print xprint(x)# Conversion into matrixz<-as.matrix(x)# Print zprint(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"


Please Login to comment...