VOOZH about

URL: https://www.geeksforgeeks.org/r-language/conversion-functions-in-r-programming/

⇱ Conversion Functions in R Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Conversion Functions in R Programming

Last Updated : 12 Jul, 2025
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: 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: 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: 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: 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: 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: 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: 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: 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"
    
Comment

Explore