File: data_modify.Rd

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 5,354 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data_modify.R
\name{data_modify}
\alias{data_modify}
\alias{data_modify.data.frame}
\title{Create new variables in a data frame}
\usage{
data_modify(data, ...)

\method{data_modify}{data.frame}(data, ..., .if = NULL, .at = NULL, .modify = NULL)
}
\arguments{
\item{data}{A data frame}

\item{...}{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:
\itemize{
\item A sequence of named, literal expressions, where the left-hand side refers
to the name of the new variable, while the right-hand side represent the
values of the new variable. Example: \code{Sepal.Width = center(Sepal.Width)}.
\item A sequence of string values, representing expressions.
\item A variable that contains a string representation of the expression. Example:

\if{html}{\out{<div class="sourceCode r">}}\preformatted{a <- "2 * Sepal.Width"
data_modify(iris, a)
}\if{html}{\out{</div>}}
\item A character vector of expressions. Example:
\code{c("SW_double = 2 * Sepal.Width", "SW_fraction = SW_double / 10")}. This
type of expression cannot be mixed with other expressions, i.e. if a
character vector is provided, you may not add further elements to \code{...}.
\item Using \code{NULL} as right-hand side removes a variable from the data frame.
Example: \code{Petal.Width = NULL}.
\item For data frames (including grouped ones), the function \code{n()} can be used to count the
number of observations and thereby, for instance, create index values by
using \code{id = 1:n()} or \code{id = 3:(n()+2)} and similar.
}

Note that newly created variables can be used in subsequent expressions,
including \code{.at} or \code{.if}. See also 'Examples'.}

\item{.if}{A function that returns \code{TRUE} for columns in the data frame where
\code{.if} applies. This argument is used in combination with the \code{.modify} argument.
Note that only one of \code{.at} or \code{.if} can be provided, but not both at the same
time. Newly created variables in \code{...} can also be selected, see 'Examples'.}

\item{.at}{A character vector of variable names that should be modified. This
argument is used in combination with the \code{.modify} argument. Note that only one
of \code{.at} or \code{.if} can be provided, but not both at the same time. Newly created
variables in \code{...} can also be selected, see 'Examples'.}

\item{.modify}{A function that modifies the variables defined in \code{.at} or \code{.if}.
This argument is used in combination with either the \code{.at} or the \code{.if} argument.
Note that the modified variable (i.e. the result from \code{.modify}) must be either
of length 1 or of same length as the input variable.}
}
\description{
Create new variables or modify existing variables in a data frame. Unlike \code{base::transform()}, \code{data_modify()}
can be used on grouped data frames, and newly created variables can be directly
used.
}
\note{
\code{data_modify()} can also be used inside functions. However, it is
recommended to pass the recode-expression as character vector or list of
characters.
}
\examples{
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),
  id = 1:n()
)
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)

}