Variables in Crunch datasets are organized into folders, like in a file
system. These functions allow you to create new folders and move objects into
folders. Their names, mv
and mkdir
, suggest their Unix file utility
inspiration.
mv(dataset, variables, path)mkdir(dataset, path, variables = NULL)
A Crunch dataset
A Variable, selection of variables from dataset
, or any
other object that can be moved to a folder. For mkdir
, variables
is
optional.
A character "path" to the folder: either a
vector of nested folder names or a single string with nested folders
separated by a delimiter ("/" default, configurable via
options(crunch.delimiter)
)
dataset
, with the folder at path
guaranteed to be created and
variables
, if specified, moved into it.
The functions have some differences from the strict behavior of their Unix
ancestors. For one, they work recursively, without additional arguments:
mkdir
will make every directory necessary to construct the requested path,
even if all parent directories didn't already exist; and mv
doesn't
require that the directory to move to already exist. mkdir
also takes an
optional variables
argument, which allows you to say "make this directory
and put these variables in it" in one command. As a result of these
behaviors, mv
and mkdir
here are essentially the same, just with
arguments in reverse order: mv
is move "variables" to "path", while mkdir
is create "path" and put "variables" in it.
Both functions take "dataset" as the first argument, so they naturally
support pipelining (as with the %>%
operator).
# NOT RUN {
ds <- loadDataset("Example survey")
ds <- mv(ds, c("gender", "age", "educ"), "Demographics")
ds <- mkdir(ds, "Key Performance Indicators/Brand X")
# These can also be chained together
require(magrittr)
ds <- ds %>%
mv(c("aware_x", "nps_x"), "Key Performance Indicators/Brand X") %>%
mv(c("aware_y", "nps_y"), "Key Performance Indicators/Brand Y")
# }
Run the code above in your browser using DataLab