Skip to content

Commit

Permalink
tests duplicateCol using columns' names 🛡️
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed Jun 13, 2018
1 parent 9c6d6c7 commit 2153206
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 44 deletions.
18 changes: 11 additions & 7 deletions R/duplicateRow.R
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]
}
14 changes: 8 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ output:

```{r echo=FALSE, message = FALSE}
knitr::opts_chunk$set(
fig.path = "inst/assets/img/",
fig.path = "inst/assets/fig/",
comment = "R>> ",
collapse = TRUE,
warning = FALSE,
message = FALSE,
fig.width = 4,
fig.height = 4
fig.width = 5,
fig.height = 5
)
devtools::load_all(".")
```


## letiRmisc
# letiRmisc

### Short description

## About

### Description

The *inSilecoMisc* package is a set of useful R functions created to ease some
operations we often do.
Expand Down
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
letiRmisc
---------
=========

About
-----

### Short description
### Description

The *inSilecoMisc* package is a set of useful R functions created to
ease some operations we often do. Functions here are written to be used
Expand Down Expand Up @@ -53,16 +56,16 @@ Main features
### Assign a category

(seqv <- stats::runif(40))
R>> [1] 0.13780573 0.17708009 0.77330267 0.30710906 0.67434486 0.55012283
R>> [7] 0.63048250 0.79462166 0.23519578 0.78091285 0.32436219 0.45307328
R>> [13] 0.35748185 0.24681598 0.76751976 0.66642766 0.96600211 0.04952655
R>> [19] 0.03451996 0.34324260 0.14052181 0.85171006 0.98864700 0.88616456
R>> [25] 0.96326465 0.12279069 0.85347725 0.39668267 0.65347731 0.86984038
R>> [31] 0.09091806 0.02286843 0.27840141 0.70495423 0.55510063 0.03851234
R>> [37] 0.84837195 0.33887783 0.35538841 0.88467788
R>> [1] 0.99214337 0.31171799 0.56070470 0.54466872 0.29966394 0.32109119
R>> [7] 0.28128818 0.59589856 0.81216719 0.29817815 0.26570390 0.67276404
R>> [13] 0.94476824 0.36332951 0.92936313 0.23618996 0.50067110 0.29241868
R>> [19] 0.21714723 0.44384743 0.22142397 0.77767900 0.67439501 0.45173673
R>> [25] 0.27326727 0.66848496 0.80267346 0.56277968 0.96802984 0.10215451
R>> [31] 0.56928679 0.97048877 0.43552671 0.09509066 0.67638032 0.06425101
R>> [37] 0.06244420 0.55870936 0.60233759 0.32699378
categorize(seqv, categ=seq(0.1,0.9, 0.1))
R>> [1] 2 2 8 4 7 6 7 8 3 8 4 5 4 3 8 7 10 1 1 4 2 9 10
R>> [24] 9 10 2 9 4 7 9 1 1 3 8 6 1 9 4 4 9
R>> [1] 10 4 6 6 3 4 3 6 9 3 3 7 10 4 10 3 6 3 3 5 3 8 7
R>> [24] 5 3 7 9 6 10 2 6 10 5 1 7 1 1 6 7 4

### Turn a matrix or a data frame into a squared matrix

Expand Down Expand Up @@ -90,11 +93,11 @@ Main features
df1 <- matrix(signif(runif(20),4), ncol=2)
df2 <- assignClass2df(df1, 2, 'character')
str(df1)
R>> num [1:10, 1:2] 0.183 0.128 0.652 0.228 0.919 ...
R>> num [1:10, 1:2] 0.341 0.786 0.639 0.343 0.919 ...
str(df2)
R>> 'data.frame': 10 obs. of 2 variables:
R>> $ V1: num 0.183 0.128 0.652 0.228 0.919 ...
R>> $ V2: chr "0.01379" "0.09421" "0.3086" "0.4628" ...
R>> $ V1: num 0.341 0.786 0.639 0.343 0.919 ...
R>> $ V2: chr "0.03802" "0.9461" "0.6638" "0.9869" ...

### Assign a symbol to a p-value

Expand Down
6 changes: 3 additions & 3 deletions man/duplicateRow.Rd

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

30 changes: 16 additions & 14 deletions tests/testthat/test-duplicate.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
context("Duplicate rows and columns")

mydf <- data.frame(
var1=1:10,
var2=LETTERS[1:10],
var3=LETTERS[1:10]
var1 = 1:10,
var2 = LETTERS[1:10],
var3 = LETTERS[1:10]
)
rownames(mydf) <- letters[1:nrow(mydf)]

Expand All @@ -13,38 +13,40 @@ mydf$var3 <- as.character(mydf$var3)
mydf1a <- duplicateRow(mydf)
mydf1b <- duplicateCol(mydf)

mydf2a <- duplicateRow(mydf, id.el=11)
mydf2a <- duplicateRow(mydf, id.el = 11)

mydf3a <- duplicateRow(mydf, id.el=c(2,6), times=c(1,2))
mydf3b <- duplicateRow(mydf, id.el=c("b", "f"), times=c(1,2))
mydf3c <- duplicateCol(mydf, id.el=c(2,3), times=c(1,2))
mydf3a <- duplicateRow(mydf, id.el = c(2, 6), times = c(1,2))
mydf3b <- duplicateRow(mydf, id.el = c("b", "f"), times = c(1,2))
mydf3c <- duplicateCol(mydf, id.el = c(2, 3), times =c(1,2))
mydf3d <- duplicateCol(mydf, id.el = c("var2", "var3"), times =c(1,2))
#
mydf4a <- duplicateRow(mydf, append = TRUE)
mydf4b <- duplicateCol(mydf, append = TRUE)


test_that("duplicates by default", {
expect_equal(nrow(mydf1a), nrow(mydf)+1)
expect_equal(all(mydf1a[1,]==mydf1a[2,]), TRUE)
expect_equal(all(mydf1a[1,] == mydf1a[2,]), TRUE)
expect_equal(ncol(mydf1b), ncol(mydf)+1)
})

test_that("duplicates missings", {
expect_equal(mydf2a[11,1], NA_integer_)
expect_equal(as.numeric(mydf2a[11,2]), NA_integer_)
expect_equal(mydf2a[11,3], NA_character_)
expect_error(duplicateCol(mydf, id.el=4))
expect_equal(mydf2a[11, 1], NA_integer_)
expect_equal(as.numeric(mydf2a[11, 2]), NA_integer_)
expect_equal(mydf2a[11, 3], NA_character_)
expect_error(duplicateCol(mydf, id.el = 4))
})

test_that("duplicates multi elements", {
expect_equal(nrow(mydf3a), nrow(mydf)+3)
expect_true(identical(mydf3b, mydf3a))
expect_equal(ncol(mydf3c), ncol(mydf)+3)
expect_true(identical(mydf3c, mydf3d))
})

test_that("duplicates and appends", {
expect_equal(nrow(mydf4a), nrow(mydf)+1)
expect_equal(ncol(mydf4b), ncol(mydf)+1)
expect_equal(all(mydf4a[1,]==mydf4a[11,]), TRUE)
expect_equal(all(mydf4b[,1]==mydf4b[,4]), TRUE)
expect_equal(all(mydf4a[1,] == mydf4a[11,]), TRUE)
expect_equal(all(mydf4b[, 1] == mydf4b[, 4]), TRUE)
})

0 comments on commit 2153206

Please sign in to comment.