data_modify | R Documentation |
Create new variables or modify existing variables in a data frame. Unlike base::transform()
, data_modify()
can be used on grouped data frames, and newly created variables can be directly
used.
data_modify(data, ...)
## S3 method for class 'data.frame'
data_modify(data, ..., .if = NULL, .at = NULL, .modify = NULL)
data |
A data frame |
... |
One or more expressions that define the new variable name and the values or recoding of those new variables. These expressions can be one of:
Note that newly created variables can be used in subsequent expressions,
including |
.if |
A function that returns |
.at |
A character vector of variable names that should be modified. This
argument is used in combination with the |
.modify |
A function that modifies the variables defined in |
data_modify()
can also be used inside functions. However, it is
recommended to pass the recode-expression as character vector or list of
characters.
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,
as_expr("c12hour_c = center(c12hour)"),
as_expr("c12hour_z = c12hour_c / sd(c12hour, na.rm = TRUE)"),
as_expr("c12hour_z2 = standardize(c12hour)")
)
head(new_efc)
# using a character vector, provided a variable
xpr <- c(
"c12hour_c = center(c12hour)",
"c12hour_z = c12hour_c / sd(c12hour, na.rm = TRUE)",
"c12hour_z2 = standardize(c12hour)"
)
new_efc <- data_modify(efc, as_expr(xpr))
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 = as_expr(stand)
)
head(new_efc)
# attributes - in this case, value and variable labels - are preserved
str(new_efc)
# using `paste()` to build a string-expression
to_standardize <- c("Petal.Length", "Sepal.Length")
out <- data_modify(
iris,
as_expr(
paste0(to_standardize, "_stand = standardize(", to_standardize, ")")
)
)
head(out)
# 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),
id = 1:n()
)
head(new_efc)
# works from inside functions
foo1 <- function(data, ...) {
head(data_modify(data, ...))
}
foo1(iris, SW_fraction = Sepal.Width / 10)
# or
foo1(iris, as_expr("SW_fraction = Sepal.Width / 10"))
# also with string arguments, using `as_expr()`
foo2 <- function(data, modification) {
head(data_modify(data, as_expr(modification)))
}
foo2(iris, "SW_fraction = Sepal.Width / 10")
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.