Skip to content

Commit

Permalink
mat2df
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangchuangYu committed Dec 20, 2022
1 parent 72e4d7f commit 9f7561f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: yulab.utils
Title: Supporting Functions for Packages Maintained by 'YuLab-SMU'
Version: 0.0.5.001
Version: 0.0.6
Authors@R: c(person("Guangchuang", "Yu", email = "guangchuangyu@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6485-8781")))
Description: Miscellaneous functions commonly used by 'YuLab-SMU'.
Imports:
Expand All @@ -10,4 +10,4 @@ ByteCompile: true
License: Artistic-2.0
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(get_fun_from_pkg)
export(install_zip)
export(install_zip_gh)
export(is.installed)
export(mat2df)
export(mypkg)
export(o)
export(read.cb)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# yulab.utils 0.0.5.001
# yulab.utils 0.0.6

+ `mat2df()` to convert matrix to a tidy data frame (2022-12-20, Tue)
+ `str_starts()` and `str_ends()` (2022-07-29, Fri)

# yulab.utils 0.0.5
Expand Down
22 changes: 22 additions & 0 deletions R/mat2df.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
##' convert a matrix to a tidy data frame (from wide to long format as described in the tidyverse concetp)
##'
##'
##' @title mat2df
##' @param x the input matrix
##' @return a data.frame in long format with the 'value' column stores the original values and 'row' and 'col' columns stored in row and column index as in x
##' @examples
##' x <- matrix(1:15, nrow = 3)
##' mat2df(x)
##' @export
##' @author Guangchuang Yu
mat2df <- function(x) {
nr <- nrow(x)
nc <- ncol(x)
d <- data.frame(
value = as.vector(x),
row = rep(1:nr, times = nc),
col = rep(1:nc, each = nr)
)
return(d)
}

24 changes: 24 additions & 0 deletions man/mat2df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f7561f

Please sign in to comment.