Skip to content
Related Articles

Related Articles

Improve Article

Data Type Conversion in R

  • Last Updated : 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 assignment
name <- "GeeksforGeeks"
age <- 20
pwd <- FALSE
  
# type checking
typeof(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.

Data Type Conversion in R

Numeric or Character to Logical type:

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 assignment
age <- 20
pwd <- "FALSE"
  
# Converting type
as.logical(age)
as.logical(pwd)

Output:

[1] TRUE
[1] FALSE

Numeric or Logical to Character type:

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 assignment
age <- 20
pwd <- FALSE
  
# Converting type
as.character(age)
as.character(pwd)

Output:

[1] "20"
[1] "FALSE"

Character or Logical to Numeric type:

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 assignment
age <- "20"
pwd <- FALSE
  
# Converting type
as.numeric(age)
as.numeric(pwd)

Output:

[1] 20
[1] 0

Vectors to Matrix:

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 vectors
vector1 <- 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"    

Vectors to Dataframe:

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 vectors
vector1 <- 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

Matrix to Vector:

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 matrix
mat<- 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

Matrix to Dataframe:

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 matrix
mat<- 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

Dataframe to Matrix:

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 dataframe
df <- 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"



My Personal Notes arrow_drop_up
Recommended Articles
Page :