Suppose my data frame has columns A, B, C, D, E.
I want to produce a data frame with columns A, B, C, X, where X = D * E.
Obviously I can use %>% mutate(X = D * E) %>% select (-D, -E)
, but for more elaborate situations, is there a way to do it in a single command? Like transmute()
, but only discarding the columns that were mentioned.
Silly, but I keep wishing for this bit of conciseness.
dplyr select() function is used to select the column and by using negation of this to remove columns. All verbs in dplyr package take data.
mutate() adds new variables and preserves existing ones; transmute() adds new variables and drops existing ones. New variables overwrite existing variables of the same name.
%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).
There are now an experimental method added to mutate which allows you to do this in one operation:
df %>% mutate(X = D * E, .keep = "unused")
It is also possible to specify where the new variables goes between the others. See https://rdrr.io/github/tidyverse/dplyr/man/mutate.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With