Data Type Conversion in R - GeeksforGeeks

21 Apr, 2021
Prerequisite: Data Types in the R
Data Type conversion is the process of converting one type of data to another type of data. R Programming Language has only 3 data types: Numeric, Logical, Character. In this article, we are going to see how to convert the data type in the R Programming language
Since R is a weakly typed language or dynamically typed language, R language automatically creates data types based on the values assigned to the variable. We see a glimpse of this in the code below:
R
# value assignmentname <- "GeeksforGeeks"age <- 20pwd <- FALSE  # type checkingtypeof(name)typeof(age)typeof(pwd)
Output:
[1] "character"
[1] "double"
[1] "logical"
We have used typeof(value) function to check the type of the values assigned to the variables in the above code.
Any numeric value which is not 0, on conversion to Logical type gets converted to TRUE. Here “20” is a non-zero numeric value. Hence, it gets converted to TRUE.  
“FALSE” is a character type which on conversion becomes FALSE of logical type. The double quotes are removed on conversion from character type to Logical type.
Syntax: as.logical(value)
Example:
R
# value assignmentage <- 20pwd <- "FALSE"  # Converting typeas.logical(age)as.logical(pwd)
Output:
[1] TRUE
[1] FALSE
Any character type values are always enclosed within double quotes(” “).  Hence, the conversion of 20 of numeric type gets converted into “20” of character type. Similarly, converting the FALSE of logical type into character type gives us “FALSE”.
Syntax: as.character(value)
Example:
R
# value assignmentage <- 20pwd <- FALSE  # Converting typeas.character(age)as.character(pwd)
Output:
[1] "20"
[1] "FALSE"
Syntax: as.numeric(value)
 “20” of character type on being converted to numeric type becomes 20, just the double quotes got removed. Conversion of Logical type to Numeric, FALSE-> 0 and TRUE-> 1.
Example:
R
# value assignmentage <- "20"pwd <- FALSE  # Converting typeas.numeric(age)as.numeric(pwd)
Output:
[1] 20
[1] 0
In the code below, we have converted 2 sample vectors into a single matrix by using the syntax below. The elements of the vectors are filled in Row major order.
Syntax: rbind(vector1, vector2, vector3.....vectorN)
We have converted 2 sample vectors into a single matrix by using the syntax below. The elements of the vectors are filled in Column major order.
Syntax:  cbind(vector1, vector2, vector3.....vectorN)
Example:
R
# sample vectorsvector1 <- c('red','green',"blue","yellow")vector2 <- c(1,2,3,4)  print("Row Major Order")rbind(vector1,vector2)print("Column Major Order")cbind(vector1,vector2)
Output:
[1] "Row Major Order"
        [,1]  [,2]    [,3]   [,4]    
vector1 "red" "green" "blue" "yellow"
vector2 "1"   "2"     "3"    "4"   
  
[1] "Column Major Order"
     vector1  vector2
[1,] "red"    "1"    
[2,] "green"  "2"    
[3,] "blue"   "3"    
[4,] "yellow" "4"    
In the code below, on the conversion of our sample vectors into dataframe, elements are filled in the column-major order. The first vector becomes the 1st column, the second vector became the 2nd column.
Syntax: data.frame(vector1, vector2, vector3.....vectorN)  
Example:
R
# sample vectorsvector1 <- c('red', 'green', "blue", "yellow")vector2 <- c(1, 2, 3, 4)  data.frame(vector1, vector2)
Output:
 vector1 vector2
1     red       1
2   green       2
3    blue       3
4  yellow       4
In the code below, the sample matrix is containing elements from 1 to 6, we have specified nrows=2 which means our sample matrix will be containing 2 rows, then we have used the syntax below which converts the matrix into one long vector. The elements of the matrix are accessed in column-major order. 
Syntax: as.vector(matrix_name)
Example:
R
# sample matrixmat<- matrix(c(1:6), nrow = 2)print("Sample Matrix")mat  print("After conversion into vector")as.vector(mat)
Output:
[1] "Sample Matrix"
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[1] "After conversion into vector"
[1] 1 2 3 4 5 6
In the code below, the sample matrix is containing elements from 1 to 6, we have specified nrows=2 which means our sample matrix will be containing 2 rows, then we have used the syntax below which converts the matrix into a dataframe. The elements of the matrix are accessed in column-major order. 
Syntax: as.data.frame(matrix_name)
Example:
R
# sample matrixmat<- matrix(c(1:6), nrow = 2)print("Sample Matrix")mat  print("After conversion into Dataframe")as.data.frame(mat)
Output:
[1] "Sample Matrix"
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[1] "After conversion into Dataframe"
  V1 V2 V3
1  1  3  5
2  2  4  6
In the code below, we have created a sample dataframe containing elements of different types, then we have used the syntax below which converts the dataframe into a matrix of character type, which means each element of the matrix is of character type.
Syntax: as.matrix(dataframe_name)
Example:
R
# sample dataframedf <- data.frame(   serial = c (1:5),    name = c("Welcome","to","Geeks","for","Geeks"),   stipend = c(2000,3000.5,5000,4000,500.2),    stringsAsFactors = FALSE)  print("Sample Dataframe")df  print("After conversion into Matrix")as.matrix(df)
Output:
[1] "Sample Dataframe"
  serial    name stipend
1      1 Welcome  2000.0
2      2      to  3000.5
3      3   Geeks  5000.0
4      4     for  4000.0
5      5   Geeks   500.2

[1] "After conversion into Matrix"
     serial name      stipend 
[1,] "1"    "Welcome" "2000.0"
[2,] "2"    "to"      "3000.5"
[3,] "3"    "Geeks"   "5000.0"
[4,] "4"    "for"     "4000.0"
[5,] "5"    "Geeks"   " 500.2"
Picked
R Data-types
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
Data Visualization in R
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
How to import an Excel File into R ?
How to filter R DataFrame by values in a column?