Data Type Conversion in R
Last Updated :
12 Jul, 2025
Data Type conversion refers to changing data from one type to another. In R, the primary data types are Numeric, Logical and Character. R provides built-in functions such as as.numeric(), as.character(), as.logical() etc to perform these conversions.
Implementation of Data Type Conversion in R
We demonstrate how to convert data types in R programming language.
1. Numeric or Character to Logical Type
We use as.logical() to convert numeric or character values into logical type. The resulting logical values are either TRUE or FALSE. For example, numeric 1 becomes TRUE, numeric 0 becomes FALSE, and character "TRUE" becomes TRUE.
Syntax:
as.logical(value)
Where:
- as.logical: Converts value to logical type.
Example:
R
age <- 20
pwd <- "FALSE"
as.logical(age)
as.logical(pwd)
Output:
[1] TRUE
[1] FALSE
2. Numeric or Logical to Character type
We use as.character() to convert numeric or logical values into character type. The resulting character values are enclosed in double quotes ("). For example, numeric 20 becomes "20" and logical FALSE becomes "FALSE".
Syntax:
as.character(value)
Where:
- as.character: Converts value to character type.
Example:
R
age <- 20
pwd <- FALSE
as.character(age)
as.character(pwd)
Output:
[1] "20"
[1] "FALSE"
3. Character or Logical to Numeric type
We use as.numeric() to convert logical values where TRUE becomes 1 and FALSE becomes 0, while character values are converted to their numeric equivalent, with non-numeric characters resulting in NA.
Syntax:
as.numeric(value)
Where:
- as.numeric: Converts a value to numeric type.
Example:
R
age <- "20"
pwd <- FALSE
as.numeric(age)
as.numeric(pwd)
Output:
[1] 20
[1] 0
4. Vectors to Matrix
We use rbind() to combine multiple vectors into a matrix by binding them row-wise. Each vector becomes a row in the resulting matrix. All vectors should be of the same length to avoid recycling or errors.
Syntax:
rbind(vector1, vector2, vector3.....vectorN)
Where:
- rbind: Combines multiple vectors or data frames by rows.
We can also combine multiple vectors into a matrix by binding them column-wise using the cbind() function.
Syntax:
cbind(vector1, vector2, vector3.....vectorN)
Where:
- cbind: Combines multiple vectors or data frames by columns.
Example:
R
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:
Output5. Vectors to Data Frame
We use data.frame() to convert multiple vectors into a data frame. Each vector becomes a separate column in the data frame. All vectors should be of equal length to maintain the structure.
Syntax:
data.frame(vector1, vector2, vector3.....vectorN)
Where:
- data.frame: Creates a data frame by combining multiple vectors or variables into columns.
Example:
R
vector1 <- c('red', 'green', "blue", "yellow")
vector2 <- c(1, 2, 3, 4)
data.frame(vector1, vector2)
Output:
Output6. Matrix to Vector
We use as.vector() to convert a matrix into a single long vector. The elements of the matrix are accessed and filled into the vector in column-major order.
Syntax:
as.vector(matrix_name)
Where:
- as.vector: Converts a matrix or array into a vector.
Example:
R
mat<- matrix(c(1:6), nrow = 2)
print("Sample Matrix")
mat
print("After conversion into vector")
as.vector(mat)
Output:
Output7. Matrix to Data Frame
We use as.data.frame() to convert a matrix into a data frame. Each column of the matrix becomes a column in the data frame and the elements are filled in column-major order.
Syntax:
as.data.frame(matrix_name)
Where:
- data.frame: Creates a data frame by combining multiple vectors or variables into columns.
Example:
R
mat<- matrix(c(1:6), nrow = 2)
print("Sample Matrix")
mat
print("After conversion into Dataframe")
as.data.frame(mat)
Output:
Output8. Data Frame to Matrix
We use as.matrix() to convert a data frame into a matrix. If the data frame contains elements of different data types (e.g., numeric and character), all elements in the resulting matrix are converted to character type to maintain consistency.
Syntax:
as.matrix(dataframe_name)
Where:
- as.matrix: Converts a data frame to a matrix.
Example:
R
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:
OutputThe output shows that after converting the data frame to a matrix, all columns are coerced to the character type, as matrices in R require uniform data types, even if some columns originally had numeric values.
Data Type Conversion in R
Similar Reads
Data Type Conversion in MATLAB Data Types in MATLAB is the upheld information organizes that are utilized for calculation. MATLAB is a well-known numerical and factual information investigation instrument that has a large number of elements for calculation. The different kinds of data type MATLAB supports are numeric types, chara
3 min read
Convert DataFrame to vector in R In this article, we will discuss how a dataframe can be converted to a vector in R. For the Conversion of dataframe into a vector, we can simply pass the dataframe column name as [[index]]. Approach: We are taking a column in the dataframe and passing it into another variable by the selection method
2 min read
Convert Tibble to Data Frame in R Tibbles are a type of data frame in R Programming Language that has an enhanced print method, making it easier to display data. However, in some situations, we may need to convert a tibble to a data frame. So, in this article, we will explore some approaches to convert tibbles to data frames. Tibble
4 min read
How to Check Data Type in R? R programming has data types that play a very significant role in determining how data is documented, modified, and analyzed. Knowing the data type of an object is an essential part of most data analysis and programming. The current article delivers a step-by-step guide that will familiarize you wit
4 min read
Convert dataframe to data.table in R In this article, we will discuss how to convert dataframe to data.table in R Programming Language. data.table is an R package that provides an enhanced version of dataframe. Characteristics of data.table :Â data.table doesnât set or use row namesrow numbers are printed with a : for better readabilit
5 min read
R-Data Types Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.R Programming lang
5 min read