Repository navigation
Expand file tree
/
Copy pathmutate.Rd
More file actions
167 lines (137 loc) · 5.78 KB
/
Copy pathmutate.Rd
File metadata and controls
167 lines (137 loc) · 5.78 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/manip.r
\name{mutate}
\alias{mutate}
\alias{transmute}
\title{Create or transform variables}
\usage{
mutate(.data, ...)
transmute(.data, ...)
}
\arguments{
\item{.data}{A tbl. All main verbs are S3 generics and provide methods
for \code{\link[=tbl_df]{tbl_df()}}, \code{\link[dtplyr:tbl_dt]{dtplyr::tbl_dt()}} and \code{\link[dbplyr:tbl_dbi]{dbplyr::tbl_dbi()}}.}
\item{...}{Name-value pairs of expressions, each with length 1 or the same
length as the number of rows in the group (if using \code{\link[=group_by]{group_by()}}) or in the entire
input (if not using groups). The name of each argument will be the name of
a new variable, and the value will be its corresponding value. Use a \code{NULL}
value in \code{mutate} to drop a variable. New variables overwrite existing variables
of the same name.
The arguments in \code{...} are automatically \link[rlang:quo]{quoted} and
\link[rlang:eval_tidy]{evaluated} in the context of the data
frame. They support \link[rlang:quasiquotation]{unquoting} and
splicing. See \code{vignette("programming")} for an introduction to
these concepts.}
}
\value{
An object of the same class as \code{.data}.
}
\description{
\code{mutate()} adds new variables and preserves existing ones;
\code{transmute()} adds new variables and drops existing ones. Both
functions preserve the number of rows of the input.
New variables overwrite existing variables of the same name.
}
\section{Useful functions available in calculations of variables}{
\itemize{
\item \code{\link{+}}, \code{\link{-}}, \code{\link[=log]{log()}}, etc., for their usual mathematical meanings
\item \code{\link[=lead]{lead()}}, \code{\link[=lag]{lag()}}
\item \code{\link[=dense_rank]{dense_rank()}}, \code{\link[=min_rank]{min_rank()}}, \code{\link[=percent_rank]{percent_rank()}}, \code{\link[=row_number]{row_number()}},
\code{\link[=cume_dist]{cume_dist()}}, \code{\link[=ntile]{ntile()}}
\item \code{\link[=cumsum]{cumsum()}}, \code{\link[=cummean]{cummean()}}, \code{\link[=cummin]{cummin()}}, \code{\link[=cummax]{cummax()}}, \code{\link[=cumany]{cumany()}}, \code{\link[=cumall]{cumall()}}
\item \code{\link[=na_if]{na_if()}}, \code{\link[=coalesce]{coalesce()}}
\item \code{\link[=if_else]{if_else()}}, \code{\link[=recode]{recode()}}, \code{\link[=case_when]{case_when()}}
}
}
\section{Grouped tibbles}{
Because mutating expressions are computed within groups, they may
yield different results on grouped tibbles. This will be the case
as soon as an aggregating, lagging, or ranking function is
involved. Compare this ungrouped mutate:\preformatted{starwars \%>\%
mutate(mass / mean(mass, na.rm = TRUE)) \%>\%
pull()
}
With the grouped equivalent:\preformatted{starwars \%>\%
group_by(gender) \%>\%
mutate(mass / mean(mass, na.rm = TRUE)) \%>\%
pull()
}
The former normalises \code{mass} by the global average whereas the
latter normalises by the averages within gender levels.
Note that you can't overwrite a grouping variable within
\code{mutate()}.
\code{mutate()} does not evaluate the expressions when the group is empty.
}
\section{Scoped mutation and transmutation}{
The three \link{scoped} variants of \code{mutate()} (\code{\link[=mutate_all]{mutate_all()}},
\code{\link[=mutate_if]{mutate_if()}} and \code{\link[=mutate_at]{mutate_at()}}) and the three variants of
\code{transmute()} (\code{\link[=transmute_all]{transmute_all()}}, \code{\link[=transmute_if]{transmute_if()}},
\code{\link[=transmute_at]{transmute_at()}}) make it easy to apply a transformation to a
selection of variables.
}
\section{Tidy data}{
When applied to a data frame, row names are silently dropped. To preserve,
convert to an explicit variable with \code{\link[tibble:rownames_to_column]{tibble::rownames_to_column()}}.
}
\examples{
# Newly created variables are available immediately
mtcars \%>\% as_tibble() \%>\% mutate(
cyl2 = cyl * 2,
cyl4 = cyl2 * 2
)
# You can also use mutate() to remove variables and
# modify existing variables
mtcars \%>\% as_tibble() \%>\% mutate(
mpg = NULL,
disp = disp * 0.0163871 # convert to litres
)
# window functions are useful for grouped mutates
mtcars \%>\%
group_by(cyl) \%>\%
mutate(rank = min_rank(desc(mpg)))
# see `vignette("window-functions")` for more details
# You can drop variables by setting them to NULL
mtcars \%>\% mutate(cyl = NULL)
# mutate() vs transmute --------------------------
# mutate() keeps all existing variables
mtcars \%>\%
mutate(displ_l = disp / 61.0237)
# transmute keeps only the variables you create
mtcars \%>\%
transmute(displ_l = disp / 61.0237)
# The mutate operation may yield different results on grouped
# tibbles because the expressions are computed within groups.
# The following normalises `mass` by the global average:
starwars \%>\%
mutate(mass / mean(mass, na.rm = TRUE)) \%>\%
pull()
# Whereas this normalises `mass` by the averages within gender
# levels:
starwars \%>\%
group_by(gender) \%>\%
mutate(mass / mean(mass, na.rm = TRUE)) \%>\%
pull()
# Note that you can't overwrite grouping variables:
gdf <- mtcars \%>\% group_by(cyl)
try(mutate(gdf, cyl = cyl * 100))
# Refer to column names stored as strings with the `.data` pronoun:
vars <- c("mass", "height")
mutate(starwars, prod = .data[[vars[[1]]]] * .data[[vars[[2]]]])
# For more complex cases, knowledge of tidy evaluation and the
# unquote operator `!!` is required. See https://tidyeval.tidyverse.org/
#
# One useful and simple tidy eval technique is to use `!!` to
# bypass the data frame and its columns. Here is how to divide the
# column `mass` by an object of the same name:
mass <- 100
mutate(starwars, mass = mass / !!mass)
}
\seealso{
Other single table verbs:
\code{\link{arrange}()},
\code{\link{filter}()},
\code{\link{select}()},
\code{\link{slice}()},
\code{\link{summarise}()}
}
\concept{single table verbs}