Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > myVar := 42
- Erreur : impossible de trouver la fonction ":="
- > :=
- Erreur : unexpected assignment in ":="
- > <-
- Erreur : unexpected assignment in "<-"
- `:=` <- `<-`
- x:=3
- x
- [1] 3
- ?`<-`
- library(data.table)
- dt = data.table(col1=c(1,2,1,2,3), col2 = letters[1:5])
- dt[col1==1,col2:='b']
- ?`:=`
- library(dplyr)
- df <- tibble(
- g1 = c(1, 1, 2, 2, 2),
- g2 = c(1, 2, 1, 2, 1),
- a = sample(5),
- b = sample(5)
- )
- my_mutate <- function(df, expr) {
- expr <- enquo(expr)
- mean_name <- paste0("mean_", quo_name(expr))
- sum_name <- paste0("sum_", quo_name(expr))
- mutate(df,
- !! mean_name := mean(!! expr),
- !! sum_name := sum(!! expr)
- )
- }
- my_mutate(df, a)
- #> # A tibble: 5 x 6
- #> g1 g2 a b mean_a sum_a
- #> <dbl> <dbl> <int> <int> <dbl> <int>
- #> 1 1. 1. 1 3 3. 15
- #> 2 1. 2. 4 2 3. 15
- #> 3 2. 1. 2 1 3. 15
- #> 4 2. 2. 5 4 3. 15
- #> # ... with 1 more row
Add Comment
Please, Sign In to add comment