-
Notifications
You must be signed in to change notification settings - Fork 1
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 #55 from Metropolitan-Council/look
data.table look function
- Loading branch information
Showing
10 changed files
with
109 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#' @title Look at data.table subsets | ||
#' | ||
#' @description A function to go into the i statement of a data.table that makes random subsets based on a chosen variable | ||
#' | ||
#' @param var A column from a data.table object | ||
#' @param n numeric, How many values are used in subsetting | ||
#' @param unique logical, Randomly choose from a unique set of levels for subsetting. | ||
#' Otherwise, choose from all values despite potential repetition. | ||
#' @return A logical vector | ||
#' | ||
#' @examples | ||
#' # load some data | ||
#' library(councilR) | ||
#' library(data.table) | ||
#' titanic <- fread("https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv") | ||
#' | ||
#' # look at a random home.dest | ||
#' titanic[look(home.dest)] # re-run the this line for a different random pull | ||
#' | ||
#' # look at a random person | ||
#' titanic[look(name)] | ||
#' | ||
#' # look at a random fare and person of that fare | ||
#' titanic[look(fare)][look(embarked)] | ||
#' | ||
#' @rdname look | ||
#' @export | ||
#' | ||
look <- function(var, n = 1, unique = TRUE) { | ||
argname <- sys.call()[2] | ||
if (unique) { | ||
if (length(var) > 1) { | ||
sampleVar <- sample(unique(var), n) | ||
} else { | ||
sampleVar <- var | ||
} | ||
} else { | ||
sampleVar <- sample(var, n) | ||
} | ||
message(argname, " = ", paste(sampleVar, collapse = " • ")) | ||
var %in% sampleVar | ||
} |
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,4 +1,3 @@ | ||
|
||
## many functions here come from https://github.com/gadenbuie/js4shiny/blob/master/R/utils.R | ||
|
||
|
||
|
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.
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