Convert Character Matrix to Numeric Matrix in R
Last Updated :
09 May, 2021
In this article, we are going to see how to convert a given character matrix to numeric in R Programming Language. Converting the Character Matrix to Numeric Matrix we will use as.numeric() & matrix() Functions.
Functions Used
- as.numeric() function: This function is used to convert a given column into a numeric value column in r language.
Syntax: as.numeric(x, …)
Parameter:
x: object to be coerced.
Returns: The numeric type object in r language.
- matrix() function: This function in r language is used to create matrices.
Syntax: matrix(data, nrow, ncol, byrow, dimnames)
Parameter:
- data: is the input vector that becomes the data elements of the matrix.
- nrow: is the number of rows to be created.
- ncol: is the number of columns to be created.
- byrow: is a logical clue. If TRUE then the input vector elements are arranged by row.
- dimname: is the names assigned to the rows and columns.
Returns: It will return the matrix of the provided data to the user.
This is one of the simplest approach for converting a given character matrix to a numeric matrix, as under this approach user just have to need to call the as.numeric() function with the name of the given character matrix as its parameter and this will help the user to convert the character matrix to numeric vector and in the next step user has to call another function matrix() with the numeric vector (which was created by the as.numeric function) and in return, this function will be returning the numeric matrix to the user. By this, the user ends up the process to receive the numeric matrix from the matrix() function in r language.
Example 1:
In this example, we will be converting a given character matrix of 3 columns and 3 rows and 9 elements to a numeric matrix using as.numeric function and the matrix() function is r language.
R
# Creating character matrix
gfg_character_matrix <- matrix(c("1","2","3","4",
"5","6","7","8","9"),
ncol = 3)
print("Print character matrix")
print(gfg_character_matrix)
# Convert to numeric matrix
gfg_numeric_matrix <- matrix(
as.numeric(gfg_character_matrix), ncol = 3)
print("Print numeric matrix")
print(gfg_numeric_matrix )
Output:
[1] "Print character matrix"
[,1] [,2] [,3]
[1,] "1" "4" "7"
[2,] "2" "5" "8"
[3,] "3" "6" "9"
[1] "Print numeric matrix"
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Example 2:
In this example, we will be converting a given character matrix of 4 columns and 4 rows and 16 elements to a numeric matrix using as.numeric function and the matrix() function is r language.
R
# Creating character matrix
gfg_character_matrix <- matrix(c("-4","2","8","7","-10",
"-40","78","-54","74",
"87","0","1","41","24",
"91","11"),
ncol = 4)
print("Character matrix")
print(gfg_character_matrix)
# Convert to numeric matrix
gfg_numeric_matrix <- matrix(
as.numeric(gfg_character_matrix), ncol = 4)
print("Numeric matrix")
print(gfg_numeric_matrix )
Output:
[1] "Character matrix"
[,1] [,2] [,3] [,4]
[1,] "-4" "-10" "74" "41"
[2,] "2" "-40" "87" "24"
[3,] "8" "78" "0" "91"
[4,] "7" "-54" "1" "11"
[1] "Numeric matrix"
[,1] [,2] [,3] [,4]
[1,] -4 -10 74 41
[2,] 2 -40 87 24
[3,] 8 78 0 91
[4,] 7 -54 1 11
Similar Reads
How to Convert Character to Numeric in R?
In this article, we will discuss how to convert characters to numeric in R Programming Language. We can convert to numeric by using as.numeric() function. Syntax: as.numeric(character) where, character is an character vector Example: R # create a vector with 5 characters data = c('1', '2', '3', '4',
1 min read
How to convert DataFrame column from Character to Numeric in R ?
In this article, we will discuss how to convert DataFrame column from Character to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type c
5 min read
How to Convert Factor to Character in R?
In this article, we will discuss how to convert the Factor Class to the character Class in the R Programming Language. Method 1: Convert a Single Factor Vector to Character Vector To convert a single factor vector to a character vector we use the as.character() function of the R Language and pass th
4 min read
How to Convert Character to Factor in R?
The as.factor() method in R Programming Language is used to convert the character vector to factor class. Converting Character Vector To Factor Syntax: as.factor(char-vec) where char-vec is the character vector The class indicative of the data type of the vector can be obtained using the class() me
2 min read
Convert matrix to list in R
In this article, we will discuss how to convert a given matrix to a List in R Programming Language. Conversion of a matrix into a list in Column-major order The as.list() is an inbuilt function that takes an R language object as an argument and converts the object into a list. We have used this func
2 min read
Convert matrix or dataframe to sparse Matrix in R
Sparse matrices are in column-oriented format and they contain mostly null values. The elements in the sparse matrix which are non-null are arranged in ascending order. In this article, we will convert the matrix and dataframe to a sparse matrix in R programming language. Converting matrix to sparse
4 min read
Convert DataFrame Column to Numeric in R
In this article, we are going to see how to convert DataFrame Column to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion,
9 min read
Convert Matrix to Vector in R
In this article, we are going to convert the given matrix into the vector in R programming language. Conversion of the matrix to vector by rowMethod 1: Using c() function Simply passing the name of the matrix will do the job. Syntax: c(matrix_name) Where matrix_name is the name of the input matrix E
3 min read
Convert DataFrame to Matrix with Column Names in R
Data frames and matrices are R objects, both of which allow tabular storage of the data into well organized cells. However, the data in a data frame can consist of different data types, that is the cells may contain data belonging to a combination of data types. Matrices, on the other hand, strictly
3 min read
Convert Matrix to Dataframe in R
In this article, we will discuss how to convert the matrix into DataFrame, or we can also say that we will discuss how to create a DataFrame from a matrix in R Programming Language. A matrix can be converted to a dataframe by using a function called as.data.frame(). It will take each column from the
1 min read