data(efc)
new_efc <- data_modify(
efc,
c12hour_c = center(c12hour),
c12hour_z = c12hour_c / sd(c12hour, na.rm = TRUE),
c12hour_z2 = standardize(c12hour)
)
head(new_efc)
# using strings instead of literal expressions
new_efc <- data_modify(
efc,
"c12hour_c = center(c12hour)",
"c12hour_z = c12hour_c / sd(c12hour, na.rm = TRUE)",
"c12hour_z2 = standardize(c12hour)"
)
head(new_efc)
# using character strings, provided as variable
stand <- "c12hour_c / sd(c12hour, na.rm = TRUE)"
new_efc <- data_modify(
efc,
c12hour_c = center(c12hour),
c12hour_z = stand
)
head(new_efc)
# providing expressions as character vector
new_exp <- c(
"c12hour_c = center(c12hour)",
"c12hour_z = c12hour_c / sd(c12hour, na.rm = TRUE)"
)
new_efc <- data_modify(efc, new_exp)
head(new_efc)
# attributes - in this case, value and variable labels - are preserved
str(new_efc)
# overwrite existing variable, remove old variable
out <- data_modify(iris, Petal.Length = 1 / Sepal.Length, Sepal.Length = NULL)
head(out)
# works on grouped data
grouped_efc <- data_group(efc, "c172code")
new_efc <- data_modify(
grouped_efc,
c12hour_c = center(c12hour),
c12hour_z = c12hour_c / sd(c12hour, na.rm = TRUE),
c12hour_z2 = standardize(c12hour)
)
head(new_efc)
# works from inside functions
foo <- function(data, z) {
head(data_modify(data, z))
}
foo(iris, "var_a = Sepal.Width / 10")
new_exp <- c("SW_double = 2 * Sepal.Width", "SW_fraction = SW_double / 10")
foo(iris, new_exp)
# modify at specific positions or if condition is met
d <- iris[1:5, ]
data_modify(d, .at = "Species", .modify = as.numeric)
data_modify(d, .if = is.factor, .modify = as.numeric)
# can be combined with dots
data_modify(d, new_length = Petal.Length * 2, .at = "Species", .modify = as.numeric)
# new variables used in `.at` or `.if`
data_modify(
d,
new_length = Petal.Length * 2,
.at = c("Petal.Length", "new_length"),
.modify = round
)
# combine "extract_column_names()" and ".at" argument
out <- data_modify(
d,
.at = extract_column_names(d, select = starts_with("Sepal")),
.modify = as.factor
)
# "Sepal.Length" and "Sepal.Width" are now factors
str(out)
Run the code above in your browser using DataLab