Skip to content
Related Articles

Related Articles

Improve Article
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



My Personal Notes arrow_drop_up
Recommended Articles
Page :