Faustin Gashakamba
Faustin Gashakamba

Reputation: 171

How do I assign a dataframe to itself when starting a pipe in dplyr

Very often, I have to manipulate a data.frame through a chain of dplyr statements and then assign the result of the pipe to the original data.frame.

Example: df <- df %>% .....

Since I have the habit of naming my variables with long explicit names, this additional keystrokes might eat into my productivity a bit. I would believe there has to be a shortcut to assign a data.frame to itself without repeating oneself like this.

Any ideas?

Upvotes: 1

Views: 584

Answers (1)

Bart
Bart

Reputation: 1382

Your probably looking for %<>%:

require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
require(magrittr)
#> Loading required package: magrittr
df<-data.frame(a=1:2)
df %<>% mutate(a=a*2)
df
#>   a
#> 1 2
#> 2 4

Upvotes: 1

Related Questions