-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from hadley/feature/31-dub
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#' Converting atomic vectors to data frames | ||
#' | ||
#' A helper function that converts named atomic vectors or lists to two-column | ||
#' data frames. | ||
#' For unnamed vectors, the natural sequence is used as name column. | ||
#' | ||
#' @param x An atomic vector | ||
#' @param name,value Names of the columns that store the names and values | ||
#' | ||
#' @return A \code{\link{data_frame}} | ||
#' @export | ||
#' | ||
#' @examples | ||
#' enframe(1:3) | ||
#' enframe(c(a = 5, b = 7)) | ||
enframe <- function(x, name = "name", value = "value") { | ||
if (is.null(names(x))) { | ||
df <- data_frame(seq_along(x), x) | ||
} else { | ||
df <- data_frame(names(x), unname(x)) | ||
} | ||
names(df) <- c(name, value) | ||
df | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
context("enframe") | ||
|
||
test_that("can convert unnamed vector", { | ||
expect_identical(enframe(3:1), | ||
data_frame(name = 1:3, value = 3:1)) | ||
}) | ||
|
||
test_that("can convert named vector", { | ||
expect_identical(enframe(c(a = 2, b = 1)), | ||
data_frame(name = letters[1:2], value = as.numeric(2:1))) | ||
}) | ||
|
||
test_that("can convert zero-length vector", { | ||
expect_identical(enframe(logical()), | ||
data_frame(name = integer(), value = logical())) | ||
}) | ||
|
||
test_that("can use custom names", { | ||
expect_identical(enframe(letters, name = "index", value = "letter"), | ||
data_frame(index = seq_along(letters), | ||
letter = letters)) | ||
}) |