-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests duplicateCol using columns' names 🛡️
- Loading branch information
Kevin Cazelles
committed
Jun 13, 2018
1 parent
9c6d6c7
commit 2153206
Showing
5 changed files
with
55 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
#' Duplicate element of a dataframe. | ||
#' Duplicates elements of a dataframe. | ||
#' | ||
#' Duplicates rows and colmns of a given a dataframe. | ||
#' | ||
#' @author | ||
#' Kevin Cazelles | ||
#' | ||
#' @param x a data.frame. | ||
#' @param id.el identity of the elements to be duplicated. | ||
#' @param times number of times elements are duplicated. Could be a vector of the same length as id.el. | ||
#' @param append A logical. If \code{TRUE}, duplicated elements will be appended to the dataframe otherwise duplicated elements remain next to their parent. Non-existing columns cannot be duplicated while non-existing rows can and produce \code{NA}. | ||
#' @importFrom magrittr %>% | ||
#' @importFrom magrittr %<>% | ||
#' | ||
#' @importFrom magrittr %>% %<>% | ||
#' @export | ||
#' @examples | ||
#' data(iris, package='datasets') | ||
#' iris2 <- duplicateRow(iris, id.el=1:50, times=2) | ||
#' iris3 <- duplicateCol(iris, id.el=c('Petal.Length', 'Petal.Width'), times=c(1,2), append=TRUE) | ||
|
||
|
||
#' @describeIn duplicateRow A dataframe with duplicated rows. | ||
#' @describeIn duplicateRow returns a dataframe with duplicated rows. | ||
duplicateRow <- function(x, id.el = 1, times = 1, append = FALSE) { | ||
pos <- rep(id.el, times) %>% sort | ||
if (class(pos) == "character") | ||
ord <- c(rownames(x), pos) else ord <- c(1:nrow(x), pos) | ||
if (!append) | ||
ord %<>% sort | ||
return(x[ord, ]) | ||
|
||
x[ord, ] | ||
} | ||
|
||
#' @describeIn duplicateRow A dataframe with duplicated columns. | ||
#' @describeIn duplicateRow returns a dataframe with duplicated columns. | ||
#' @export | ||
|
||
duplicateCol <- function(x, id.el = 1, times = 1, append = FALSE) { | ||
pos <- rep(id.el, times) %>% sort | ||
if (class(pos) == "character") | ||
ord <- c(colnames(x), pos) else ord <- c(1:ncol(x), pos) | ||
if (!append) | ||
ord %<>% sort | ||
return(x[, ord]) | ||
|
||
x[, ord] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters