-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
135 additions
and
33 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
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
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
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,39 @@ | ||
#' Update variable types or values | ||
#' | ||
#' Quickly update selected variables using column names or positions. | ||
#' @param data input data | ||
#' @param ind a vector of either names or column positions of the variables to be dropped. | ||
#' @param what either a function or a non-empty character string naming the function to be called. See \link{do.call}. | ||
#' @keywords drop_columns | ||
#' @details \bold{This function updates \link{data.table} object directly.} Otherwise, output data will be returned matching input object class. | ||
#' @import data.table | ||
#' @export | ||
#' @examples | ||
#' str(update_columns(iris, 1L, as.factor)) | ||
#' str(update_columns(iris, c("Sepal.Width", "Petal.Length"), "as.integer")) | ||
#' | ||
#' ## Apply log transformation to all columns | ||
#' summary(airquality) | ||
#' summary(update_columns(airquality, names(airquality), log)) | ||
#' | ||
#' ## Force set factor to numeric | ||
#' df <- data.frame("a" = as.factor(sample.int(10L))) | ||
#' str(df) | ||
#' str(update_columns(df, "a", function(x) as.numeric(levels(x))[x])) | ||
|
||
update_columns <- function(data, ind, what) { | ||
## Check if input is data.table | ||
is_data_table <- is.data.table(data) | ||
## Detect input data class | ||
data_class <- class(data) | ||
## Set data to data.table | ||
if (!is_data_table) data <- data.table(data) | ||
## Transform columns | ||
if (is.numeric(ind)) ind <- as.integer(ind) | ||
for (j in ind) set(x = data, j = j, value = do.call(what, list(data[[j]]))) | ||
## Set data class back to original | ||
if (!is_data_table) { | ||
class(data) <- data_class | ||
return(data) | ||
} | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
context("update variables") | ||
|
||
test_that("test basic functionality", { | ||
iris_dt <- data.table(iris) | ||
update_columns(iris_dt, 1L, as.character) | ||
update_columns(iris_dt, c("Sepal.Width", "Petal.Length"), as.factor) | ||
update_columns(iris_dt, "Petal.Width", as.integer) | ||
expect_is(iris_dt[[1]], "character") | ||
expect_is(iris_dt$Sepal.Width, "factor") | ||
expect_is(iris_dt$Petal.Length, "factor") | ||
expect_is(iris_dt$Petal.Width, "integer") | ||
expect_is(iris_dt, "data.table") | ||
}) | ||
|
||
test_that("test non-data.table objects", { | ||
expect_is(update_columns(iris, 1L, as.character), "data.frame") | ||
}) |