Convert a Matrix to a Data Frame (in "Long" Format)
Source:R/matrix_to_data_frame.R
matrixToDataFrame.Rd
Convert a Matrix to a Data Frame (in "Long" Format)
Usage
matrixToDataFrame(
x,
name_row = NULL,
name_column = NULL,
name_value = "value",
row_first = TRUE
)
Arguments
- x
matrix
- name_row
name to be given to the data frame column containing the row "coordinates". Default:
names(dimnames(x))[1]
unlessNULL
,"row"
otherwise.- name_column
name to be given to the data frame column containing the column "coordinates". Default:
names(dimnames(x))[2]
unlessNULL
,"column"
otherwise.- name_value
name to be given to the data frame column containing the matrix values. Default:
value
- row_first
if
TRUE
(the default), the "row column" will come first, else the "column column".
Examples
m1 <- matrix(1:12, nrow = 3, dimnames = list(NULL, letters[1:4]))
m2 <- matrix(1:12, nrow = 3, dimnames = list(index = NULL, letters[1:4]))
m3 <- matrix(1:12, nrow = 3, dimnames = list(NULL, letter = letters[1:4]))
matrixToDataFrame(x = m1)
#> row column value
#> 1 1 a 1
#> 2 1 b 4
#> 3 1 c 7
#> 4 1 d 10
#> 5 2 a 2
#> 6 2 b 5
#> 7 2 c 8
#> 8 2 d 11
#> 9 3 a 3
#> 10 3 b 6
#> 11 3 c 9
#> 12 3 d 12
matrixToDataFrame(x = m1, row_first = FALSE)
#> column row value
#> 1 a 1 1
#> 2 a 2 2
#> 3 a 3 3
#> 4 b 1 4
#> 5 b 2 5
#> 6 b 3 6
#> 7 c 1 7
#> 8 c 2 8
#> 9 c 3 9
#> 10 d 1 10
#> 11 d 2 11
#> 12 d 3 12
matrixToDataFrame(x = m2)
#> index column value
#> 1 1 a 1
#> 2 1 b 4
#> 3 1 c 7
#> 4 1 d 10
#> 5 2 a 2
#> 6 2 b 5
#> 7 2 c 8
#> 8 2 d 11
#> 9 3 a 3
#> 10 3 b 6
#> 11 3 c 9
#> 12 3 d 12
matrixToDataFrame(x = m3)
#> row letter value
#> 1 1 a 1
#> 2 1 b 4
#> 3 1 c 7
#> 4 1 d 10
#> 5 2 a 2
#> 6 2 b 5
#> 7 2 c 8
#> 8 2 d 11
#> 9 3 a 3
#> 10 3 b 6
#> 11 3 c 9
#> 12 3 d 12
matrixToDataFrame(x = m3, "myrow", "mycol", "myval")
#> myrow mycol myval
#> 1 1 a 1
#> 2 1 b 4
#> 3 1 c 7
#> 4 1 d 10
#> 5 2 a 2
#> 6 2 b 5
#> 7 2 c 8
#> 8 2 d 11
#> 9 3 a 3
#> 10 3 b 6
#> 11 3 c 9
#> 12 3 d 12