Mutate dataset by code
mutate_dataset.Rd
Usage
mutate_dataset(x, ...)
# S3 method for TealDataset
mutate_dataset(
x,
code = character(0),
script = character(0),
vars = list(),
...
)
# S3 method for TealDatasetConnector
mutate_dataset(
x,
code = character(0),
script = character(0),
vars = list(),
...
)
# S3 method for TealDataAbstract
mutate_dataset(
x,
dataname,
code = character(0),
script = character(0),
vars = list(),
...
)
Arguments
- x
(
TealDataset
)
object.- ...
not used, only for support of S3
- code
(
character
)
Code to mutate the dataset. Must contain thedataset$dataname
. Or can also be an object of classPythonCodeClass
returned bypython_code
.- script
(
character
)
file that contains R Code that can be read usingread_script
. Preferred beforecode
argument.- vars
(named
list
))
In case when this object code depends on otherTealDataset
object(s) or other constant value, this/these object(s) should be included as named element(s) of the list. For example if this object code needsADSL
object we should specifyvars = list(ADSL = <adsl object>)
. It's recommended to includeTealDataset
orTealDatasetConnector
objects to thevars
list to preserve reproducibility. Please note thatvars
are included to this object as localvars
and they cannot be modified within another dataset.- dataname
(
character
)
Dataname to be mutated.
Examples
library(scda)
library(magrittr)
ADSL <- synthetic_cdisc_data("latest")$adsl
ADSL_dataset <- dataset(
dataname = "ADSL",
x = ADSL,
label = "AdAM subject-level dataset",
code = "ADSL <- synthetic_cdisc_data(\"latest\")$adsl"
)
ADSL_mutated <- ADSL_dataset %>%
mutate_dataset(code = "ADSL$new_variable <- 1")
ADSL_mutated$get_raw_data()$new_variable[1]
#> [1] 1
# Use an R script to mutate the data
file_example <- tempfile(fileext = ".R")
writeLines(
text = c(
"ADSL <- ADSL %>%
dplyr::mutate(new_variable = new_variable * 2)"
),
con = file_example
)
ADSL_mutated <- ADSL_mutated %>%
mutate_dataset(script = file_example)
ADSL_mutated$get_raw_data()$new_variable[1]
#> [1] 2
ADSL_mutated <- ADSL_mutated %>%
mutate_dataset(code = read_script(file_example))
ADSL_mutated$get_raw_data()$new_variable[1]
#> [1] 4