Why ifelse with character statement is not working? - r

I am having problem today to understand how functions works.
This is my code:
my_fun<- function(x){
ifelse(as.character(x) == 'Species',
a<- iris %>% select(x),
a<- iris*2)
a
}
my_fun(x = Species)
Why is it not working?

There are three changes to be made,
converting to character would be with deparse/substitute
if/else would be appropriate as ifelse requires all the arguments to be of same length
iris * 2 as an else option wouldn't work as the dataset includes a factor column as well. So, we need to multiply only those numeric columns
my_fun <- function(x) {
x <- deparse(substitute(x))
if(x == 'Species') {
iris %>%
select(all_of(x))
} else {
iris %>%
mutate(across(where(is.numeric), ~ .* 2))
}
}
-testing
my_fun(Species)
# Species
#1 setosa
#2 setosa
#3 setosa
#4 setosa
#5 setosa
#6 setosa
#...
and if we pass another input
my_fun(hello)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 10.2 7.0 2.8 0.4 setosa
#2 9.8 6.0 2.8 0.4 setosa
#3 9.4 6.4 2.6 0.4 setosa
#4 9.2 6.2 3.0 0.4 setosa
#5 10.0 7.2 2.8 0.4 setosa
#6 10.8 7.8 3.4 0.8 setosa
# ...

library(tidyverse)
my_fun <- function(x) {
if(x == "Species") {
iris %>% select(x)
} else {
iris * 2
}
}
my_fun("Species")
Why?
You have an error because Species when evaluated is not found since it was not a defined variable.
One can use Non Standard Evaluation (NSE) feature with deparse and substitute see #akrun answer. But maybe you want to call your function with a character like that my_fun("Species").
The other mistake is that ifelse is a vectorized version of if/else and here you just want to test one value (x).

If you would like to ifelse anyway, here is a variation base on answers by #akrun and #pietrodito
my_fun <- function(x) {
x <- deparse(substitute(x))
ifelse(x == "Species",
a <- list(iris %>% select(x)),
a <- list(iris * 2)
)
a[[1]]
}
which gives
> my_fun(Species)
Species
1 setosa
2 setosa
3 setosa
4 setosa
5 setosa
6 setosa
7 setosa
8 setosa
9 setosa
10 setosa
11 setosa
12 setosa
13 setosa
14 setosa
15 setosa
16 setosa
17 setosa
18 setosa
19 setosa
20 setosa
21 setosa
22 setosa
23 setosa
24 setosa
25 setosa
26 setosa
27 setosa
28 setosa
29 setosa
30 setosa
31 setosa
32 setosa
33 setosa
34 setosa
35 setosa
36 setosa
37 setosa
38 setosa
39 setosa
40 setosa
41 setosa
42 setosa
43 setosa
44 setosa
45 setosa
46 setosa
47 setosa
48 setosa
49 setosa
50 setosa
51 versicolor
52 versicolor
53 versicolor
54 versicolor
55 versicolor
56 versicolor
57 versicolor
58 versicolor
59 versicolor
60 versicolor
61 versicolor
62 versicolor
63 versicolor
64 versicolor
65 versicolor
66 versicolor
67 versicolor
68 versicolor
69 versicolor
70 versicolor
71 versicolor
72 versicolor
73 versicolor
74 versicolor
75 versicolor
76 versicolor
77 versicolor
78 versicolor
79 versicolor
80 versicolor
81 versicolor
82 versicolor
83 versicolor
84 versicolor
85 versicolor
86 versicolor
87 versicolor
88 versicolor
89 versicolor
90 versicolor
91 versicolor
92 versicolor
93 versicolor
94 versicolor
95 versicolor
96 versicolor
97 versicolor
98 versicolor
99 versicolor
100 versicolor
101 virginica
102 virginica
103 virginica
104 virginica
105 virginica
106 virginica
107 virginica
108 virginica
109 virginica
110 virginica
111 virginica
112 virginica
113 virginica
114 virginica
115 virginica
116 virginica
117 virginica
118 virginica
119 virginica
120 virginica
121 virginica
122 virginica
123 virginica
124 virginica
125 virginica
126 virginica
127 virginica
128 virginica
129 virginica
130 virginica
131 virginica
132 virginica
133 virginica
134 virginica
135 virginica
136 virginica
137 virginica
138 virginica
139 virginica
140 virginica
141 virginica
142 virginica
143 virginica
144 virginica
145 virginica
146 virginica
147 virginica
148 virginica
149 virginica
150 virginica

Related

using R to impute missing data with the mean of the available data if there was less than 10% missing data

How to use R to impute missing data with the mean of the available data across rows if there was less than 10% missing data across rows?
I would use {dplyr} and {naniar}
dplyr::mutate_if(iris2, ~ mean(is.na(.x)) > .1, naniar::impute_mean)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.100000 3.500000 1.4 0.2 setosa
#> 2 4.900000 3.072308 1.4 0.2 setosa
#> 3 4.700000 3.200000 1.3 0.2 setosa
#> 4 5.813077 3.100000 1.5 0.2 setosa
#> 5 5.000000 3.600000 1.4 0.2 setosa
#> 6 5.400000 3.900000 1.7 0.4 setosa
#> 7 4.600000 3.400000 1.4 0.3 setosa
#> 8 5.000000 3.400000 1.5 0.2 setosa
#> 9 4.400000 2.900000 1.4 0.2 setosa
#> 10 4.900000 3.100000 1.5 0.1 setosa
#> 11 5.400000 3.700000 1.5 0.2 setosa
#> 12 4.800000 3.400000 1.6 0.2 setosa
#> 13 4.800000 3.000000 1.4 0.1 setosa
#> 14 5.813077 3.000000 1.1 0.1 setosa
#> 15 5.800000 4.000000 1.2 0.2 setosa
#> 16 5.700000 4.400000 1.5 0.4 setosa
#> 17 5.400000 3.900000 1.3 0.4 setosa
#> 18 5.100000 3.500000 1.4 0.3 setosa
#> 19 5.700000 3.800000 1.7 0.3 setosa
#> 20 5.100000 3.800000 1.5 0.3 setosa
#> 21 5.400000 3.400000 1.7 0.2 setosa
#> 22 5.100000 3.700000 1.5 0.4 setosa
#> 23 4.600000 3.600000 1.0 0.2 setosa
#> 24 5.100000 3.300000 1.7 0.5 setosa
#> 25 4.800000 3.400000 1.9 0.2 setosa
#> 26 5.000000 3.000000 1.6 0.2 setosa
#> 27 5.000000 3.400000 1.6 0.4 setosa
#> 28 5.813077 3.500000 1.5 0.2 setosa
#> 29 5.200000 3.400000 1.4 0.2 setosa
#> 30 4.700000 3.200000 1.6 0.2 setosa
#> 31 4.800000 3.100000 1.6 0.2 setosa
#> 32 5.400000 3.400000 1.5 0.4 setosa
#> 33 5.200000 4.100000 1.5 0.1 setosa
#> 34 5.500000 4.200000 1.4 0.2 setosa
#> 35 4.900000 3.100000 1.5 0.2 setosa
#> 36 5.000000 3.200000 1.2 0.2 setosa
#> 37 5.500000 3.500000 1.3 0.2 setosa
#> 38 4.900000 3.600000 1.4 0.1 setosa
#> 39 4.400000 3.000000 1.3 0.2 setosa
#> 40 5.813077 3.400000 1.5 0.2 setosa
#> 41 5.000000 3.072308 1.3 0.3 setosa
#> 42 4.500000 3.072308 1.3 0.3 setosa
#> 43 4.400000 3.072308 1.3 0.2 setosa
#> 44 5.000000 3.500000 1.6 0.6 setosa
#> 45 5.100000 3.800000 1.9 0.4 setosa
#> 46 4.800000 3.000000 1.4 0.3 setosa
#> 47 5.100000 3.800000 1.6 0.2 setosa
#> 48 4.600000 3.072308 1.4 0.2 setosa
#> 49 5.300000 3.072308 1.5 0.2 setosa
#> 50 5.000000 3.300000 1.4 0.2 setosa
#> 51 7.000000 3.072308 4.7 1.4 versicolor
#> 52 6.400000 3.200000 4.5 1.5 versicolor
#> 53 6.900000 3.100000 4.9 1.5 versicolor
#> 54 5.500000 2.300000 4.0 1.3 versicolor
#> 55 6.500000 2.800000 4.6 1.5 versicolor
#> 56 5.700000 2.800000 4.5 1.3 versicolor
#> 57 6.300000 3.300000 4.7 1.6 versicolor
#> 58 4.900000 2.400000 3.3 1.0 versicolor
#> 59 6.600000 2.900000 4.6 1.3 versicolor
#> 60 5.200000 2.700000 3.9 1.4 versicolor
#> 61 5.000000 2.000000 3.5 1.0 versicolor
#> 62 5.813077 3.000000 4.2 1.5 versicolor
#> 63 6.000000 2.200000 4.0 1.0 versicolor
#> 64 6.100000 2.900000 4.7 1.4 versicolor
#> 65 5.600000 2.900000 3.6 1.3 versicolor
#> 66 6.700000 3.072308 4.4 1.4 versicolor
#> 67 5.600000 3.000000 4.5 1.5 versicolor
#> 68 5.800000 2.700000 4.1 1.0 versicolor
#> 69 6.200000 2.200000 4.5 1.5 versicolor
#> 70 5.813077 2.500000 3.9 1.1 versicolor
#> 71 5.900000 3.200000 4.8 1.8 versicolor
#> 72 6.100000 3.072308 4.0 1.3 versicolor
#> 73 6.300000 2.500000 4.9 1.5 versicolor
#> 74 6.100000 2.800000 4.7 1.2 versicolor
#> 75 6.400000 2.900000 4.3 1.3 versicolor
#> 76 6.600000 3.000000 4.4 1.4 versicolor
#> 77 6.800000 2.800000 4.8 1.4 versicolor
#> 78 6.700000 3.000000 5.0 1.7 versicolor
#> 79 5.813077 3.072308 4.5 1.5 versicolor
#> 80 5.813077 2.600000 3.5 1.0 versicolor
#> 81 5.500000 2.400000 3.8 1.1 versicolor
#> 82 5.500000 2.400000 3.7 1.0 versicolor
#> 83 5.800000 2.700000 3.9 1.2 versicolor
#> 84 6.000000 2.700000 5.1 1.6 versicolor
#> 85 5.400000 3.000000 4.5 1.5 versicolor
#> 86 6.000000 3.400000 4.5 1.6 versicolor
#> 87 6.700000 3.072308 4.7 1.5 versicolor
#> 88 6.300000 2.300000 4.4 1.3 versicolor
#> 89 5.600000 3.000000 4.1 1.3 versicolor
#> 90 5.813077 2.500000 4.0 1.3 versicolor
#> 91 5.500000 2.600000 4.4 1.2 versicolor
#> 92 6.100000 3.000000 4.6 1.4 versicolor
#> 93 5.800000 3.072308 4.0 1.2 versicolor
#> 94 5.000000 2.300000 3.3 1.0 versicolor
#> 95 5.600000 2.700000 4.2 1.3 versicolor
#> 96 5.700000 3.000000 4.2 1.2 versicolor
#> 97 5.700000 2.900000 4.2 1.3 versicolor
#> 98 5.813077 2.900000 4.3 1.3 versicolor
#> 99 5.100000 2.500000 3.0 1.1 versicolor
#> 100 5.700000 2.800000 4.1 1.3 versicolor
#> 101 5.813077 3.300000 6.0 2.5 virginica
#> 102 5.800000 3.072308 5.1 1.9 virginica
#> 103 5.813077 3.000000 5.9 2.1 virginica
#> 104 6.300000 2.900000 5.6 1.8 virginica
#> 105 6.500000 3.000000 5.8 2.2 virginica
#> 106 7.600000 3.000000 6.6 2.1 virginica
#> 107 4.900000 2.500000 4.5 1.7 virginica
#> 108 7.300000 3.072308 6.3 1.8 virginica
#> 109 6.700000 2.500000 5.8 1.8 virginica
#> 110 7.200000 3.600000 6.1 2.5 virginica
#> 111 5.813077 3.200000 5.1 2.0 virginica
#> 112 6.400000 2.700000 5.3 1.9 virginica
#> 113 6.800000 3.000000 5.5 2.1 virginica
#> 114 5.700000 2.500000 5.0 2.0 virginica
#> 115 5.800000 3.072308 5.1 2.4 virginica
#> 116 5.813077 3.200000 5.3 2.3 virginica
#> 117 6.500000 3.072308 5.5 1.8 virginica
#> 118 7.700000 3.800000 6.7 2.2 virginica
#> 119 7.700000 2.600000 6.9 2.3 virginica
#> 120 6.000000 2.200000 5.0 1.5 virginica
#> 121 6.900000 3.200000 5.7 2.3 virginica
#> 122 5.600000 3.072308 4.9 2.0 virginica
#> 123 7.700000 3.072308 6.7 2.0 virginica
#> 124 6.300000 2.700000 4.9 1.8 virginica
#> 125 6.700000 3.300000 5.7 2.1 virginica
#> 126 5.813077 3.200000 6.0 1.8 virginica
#> 127 6.200000 2.800000 4.8 1.8 virginica
#> 128 6.100000 3.000000 4.9 1.8 virginica
#> 129 6.400000 2.800000 5.6 2.1 virginica
#> 130 7.200000 3.000000 5.8 1.6 virginica
#> 131 7.400000 3.072308 6.1 1.9 virginica
#> 132 5.813077 3.800000 6.4 2.0 virginica
#> 133 5.813077 3.072308 5.6 2.2 virginica
#> 134 6.300000 2.800000 5.1 1.5 virginica
#> 135 6.100000 2.600000 5.6 1.4 virginica
#> 136 7.700000 3.000000 6.1 2.3 virginica
#> 137 5.813077 3.400000 5.6 2.4 virginica
#> 138 6.400000 3.100000 5.5 1.8 virginica
#> 139 6.000000 3.000000 4.8 1.8 virginica
#> 140 6.900000 3.100000 5.4 2.1 virginica
#> 141 6.700000 3.100000 5.6 2.4 virginica
#> 142 6.900000 3.100000 5.1 2.3 virginica
#> 143 5.813077 2.700000 5.1 1.9 virginica
#> 144 5.813077 3.200000 5.9 2.3 virginica
#> 145 6.700000 3.300000 5.7 2.5 virginica
#> 146 6.700000 3.000000 5.2 2.3 virginica
#> 147 6.300000 2.500000 5.0 1.9 virginica
#> 148 6.500000 3.000000 5.2 2.0 virginica
#> 149 6.200000 3.400000 5.4 2.3 virginica
#> 150 5.900000 3.000000 5.1 1.8 virginica
library(tidyverse)
tribble(
~a, ~b, ~c,
1, 1, 1,
3, 4, 2,
1, 1, 1,
3, 4, 2,
1, 1, 1,
3, 4, 2,
1, 1, 1,
3, NA, 2,
NA, NA, 1,
NA, NA, NA,
1, 4, 2
) |>
mutate(across(
everything(),
~ if_else(is.na(.) & mean(is.na(.)) < 0.1,
mean(., na.rm = TRUE), .
)
))
#> # A tibble: 11 × 3
#> a b c
#> <dbl> <dbl> <dbl>
#> 1 1 1 1
#> 2 3 4 2
#> 3 1 1 1
#> 4 3 4 2
#> 5 1 1 1
#> 6 3 4 2
#> 7 1 1 1
#> 8 3 NA 2
#> 9 NA NA 1
#> 10 NA NA 1.5
#> 11 1 4 2
Created on 2022-08-09 by the reprex package (v2.0.1)
Here’s a base R solution, using made-up data:
# compute % missing by row
row_pct_na <- rowSums(is.na(fakedata)) / ncol(fakedata)
# replace missings conditionally
for (i in seq_along(fakedata)) {
fakedata[[i]][is.na(fakedata[[i]]) & row_pct_na < .1] <- mean(fakedata[[i]], na.rm = TRUE)
}
Check results: rows that had <10% missing should now have 0% missing, but rows that had >=10% missing should still have same percent missing.
new_row_pct_na <- rowSums(is.na(fakedata)) / ncol(fakedata)
# before imputation
row_pct_na
# [1] 0.07692308 0.03846154 0.03846154 0.11538462 0.03846154 0.07692308
# [7] 0.00000000 0.00000000 0.00000000 0.11538462
# after imputation
new_row_pct_na
# [1] 0.0000000 0.0000000 0.0000000 0.1153846 0.0000000 0.0000000 0.0000000
# [8] 0.0000000 0.0000000 0.1153846
Data prep:
set.seed(1)
fakedata <- list()
for (letter in letters) {
fakedata[[letter]] <- rnorm(10)
fakedata[[letter]][runif(10) > .95] <- NA
}
fakedata <- as.data.frame(fakedata)

How to split a dataframe based on column class

Let's take the iris dataset for example.
I want to create two dataframes from this one. This first one would have the contiuous variables, and the second one the discrete ones.
What I do first is create a list with the category of the column
iris <- iris
a <- lapply(iris, class)
Then I create two empty lists and store the index of both discrete and continuous columns.
cont <- list()
disc <- list()
for (i in 1:length(a)){
if (a[[i]][1] == "numeric")
cont <- append(cont, i)
else
disc <- append(disc, i)
}
But I do not know how to split based on this lists.
Try the following code:
classes_iris <- sapply(iris, class)
split.default(iris, classes_iris)
Output:
$factor
Species
1 setosa
2 setosa
3 setosa
4 setosa
5 setosa
6 setosa
7 setosa
8 setosa
9 setosa
10 setosa
11 setosa
12 setosa
13 setosa
14 setosa
15 setosa
16 setosa
17 setosa
18 setosa
19 setosa
20 setosa
21 setosa
22 setosa
23 setosa
24 setosa
25 setosa
26 setosa
27 setosa
28 setosa
29 setosa
30 setosa
31 setosa
32 setosa
33 setosa
34 setosa
35 setosa
36 setosa
37 setosa
38 setosa
39 setosa
40 setosa
41 setosa
42 setosa
43 setosa
44 setosa
45 setosa
46 setosa
47 setosa
48 setosa
49 setosa
50 setosa
51 versicolor
52 versicolor
53 versicolor
54 versicolor
55 versicolor
56 versicolor
57 versicolor
58 versicolor
59 versicolor
60 versicolor
61 versicolor
62 versicolor
63 versicolor
64 versicolor
65 versicolor
66 versicolor
67 versicolor
68 versicolor
69 versicolor
70 versicolor
71 versicolor
72 versicolor
73 versicolor
74 versicolor
75 versicolor
76 versicolor
77 versicolor
78 versicolor
79 versicolor
80 versicolor
81 versicolor
82 versicolor
83 versicolor
84 versicolor
85 versicolor
86 versicolor
87 versicolor
88 versicolor
89 versicolor
90 versicolor
91 versicolor
92 versicolor
93 versicolor
94 versicolor
95 versicolor
96 versicolor
97 versicolor
98 versicolor
99 versicolor
100 versicolor
101 virginica
102 virginica
103 virginica
104 virginica
105 virginica
106 virginica
107 virginica
108 virginica
109 virginica
110 virginica
111 virginica
112 virginica
113 virginica
114 virginica
115 virginica
116 virginica
117 virginica
118 virginica
119 virginica
120 virginica
121 virginica
122 virginica
123 virginica
124 virginica
125 virginica
126 virginica
127 virginica
128 virginica
129 virginica
130 virginica
131 virginica
132 virginica
133 virginica
134 virginica
135 virginica
136 virginica
137 virginica
138 virginica
139 virginica
140 virginica
141 virginica
142 virginica
143 virginica
144 virginica
145 virginica
146 virginica
147 virginica
148 virginica
149 virginica
150 virginica
$numeric
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
7 4.6 3.4 1.4 0.3
8 5.0 3.4 1.5 0.2
9 4.4 2.9 1.4 0.2
10 4.9 3.1 1.5 0.1
11 5.4 3.7 1.5 0.2
12 4.8 3.4 1.6 0.2
13 4.8 3.0 1.4 0.1
14 4.3 3.0 1.1 0.1
15 5.8 4.0 1.2 0.2
16 5.7 4.4 1.5 0.4
17 5.4 3.9 1.3 0.4
18 5.1 3.5 1.4 0.3
19 5.7 3.8 1.7 0.3
20 5.1 3.8 1.5 0.3
21 5.4 3.4 1.7 0.2
22 5.1 3.7 1.5 0.4
23 4.6 3.6 1.0 0.2
24 5.1 3.3 1.7 0.5
25 4.8 3.4 1.9 0.2
26 5.0 3.0 1.6 0.2
27 5.0 3.4 1.6 0.4
28 5.2 3.5 1.5 0.2
29 5.2 3.4 1.4 0.2
30 4.7 3.2 1.6 0.2
31 4.8 3.1 1.6 0.2
32 5.4 3.4 1.5 0.4
33 5.2 4.1 1.5 0.1
34 5.5 4.2 1.4 0.2
35 4.9 3.1 1.5 0.2
36 5.0 3.2 1.2 0.2
37 5.5 3.5 1.3 0.2
38 4.9 3.6 1.4 0.1
39 4.4 3.0 1.3 0.2
40 5.1 3.4 1.5 0.2
41 5.0 3.5 1.3 0.3
42 4.5 2.3 1.3 0.3
43 4.4 3.2 1.3 0.2
44 5.0 3.5 1.6 0.6
45 5.1 3.8 1.9 0.4
46 4.8 3.0 1.4 0.3
47 5.1 3.8 1.6 0.2
48 4.6 3.2 1.4 0.2
49 5.3 3.7 1.5 0.2
50 5.0 3.3 1.4 0.2
51 7.0 3.2 4.7 1.4
52 6.4 3.2 4.5 1.5
53 6.9 3.1 4.9 1.5
54 5.5 2.3 4.0 1.3
55 6.5 2.8 4.6 1.5
56 5.7 2.8 4.5 1.3
57 6.3 3.3 4.7 1.6
58 4.9 2.4 3.3 1.0
59 6.6 2.9 4.6 1.3
60 5.2 2.7 3.9 1.4
61 5.0 2.0 3.5 1.0
62 5.9 3.0 4.2 1.5
63 6.0 2.2 4.0 1.0
64 6.1 2.9 4.7 1.4
65 5.6 2.9 3.6 1.3
66 6.7 3.1 4.4 1.4
67 5.6 3.0 4.5 1.5
68 5.8 2.7 4.1 1.0
69 6.2 2.2 4.5 1.5
70 5.6 2.5 3.9 1.1
71 5.9 3.2 4.8 1.8
72 6.1 2.8 4.0 1.3
73 6.3 2.5 4.9 1.5
74 6.1 2.8 4.7 1.2
75 6.4 2.9 4.3 1.3
76 6.6 3.0 4.4 1.4
77 6.8 2.8 4.8 1.4
78 6.7 3.0 5.0 1.7
79 6.0 2.9 4.5 1.5
80 5.7 2.6 3.5 1.0
81 5.5 2.4 3.8 1.1
82 5.5 2.4 3.7 1.0
83 5.8 2.7 3.9 1.2
84 6.0 2.7 5.1 1.6
85 5.4 3.0 4.5 1.5
86 6.0 3.4 4.5 1.6
87 6.7 3.1 4.7 1.5
88 6.3 2.3 4.4 1.3
89 5.6 3.0 4.1 1.3
90 5.5 2.5 4.0 1.3
91 5.5 2.6 4.4 1.2
92 6.1 3.0 4.6 1.4
93 5.8 2.6 4.0 1.2
94 5.0 2.3 3.3 1.0
95 5.6 2.7 4.2 1.3
96 5.7 3.0 4.2 1.2
97 5.7 2.9 4.2 1.3
98 6.2 2.9 4.3 1.3
99 5.1 2.5 3.0 1.1
100 5.7 2.8 4.1 1.3
101 6.3 3.3 6.0 2.5
102 5.8 2.7 5.1 1.9
103 7.1 3.0 5.9 2.1
104 6.3 2.9 5.6 1.8
105 6.5 3.0 5.8 2.2
106 7.6 3.0 6.6 2.1
107 4.9 2.5 4.5 1.7
108 7.3 2.9 6.3 1.8
109 6.7 2.5 5.8 1.8
110 7.2 3.6 6.1 2.5
111 6.5 3.2 5.1 2.0
112 6.4 2.7 5.3 1.9
113 6.8 3.0 5.5 2.1
114 5.7 2.5 5.0 2.0
115 5.8 2.8 5.1 2.4
116 6.4 3.2 5.3 2.3
117 6.5 3.0 5.5 1.8
118 7.7 3.8 6.7 2.2
119 7.7 2.6 6.9 2.3
120 6.0 2.2 5.0 1.5
121 6.9 3.2 5.7 2.3
122 5.6 2.8 4.9 2.0
123 7.7 2.8 6.7 2.0
124 6.3 2.7 4.9 1.8
125 6.7 3.3 5.7 2.1
126 7.2 3.2 6.0 1.8
127 6.2 2.8 4.8 1.8
128 6.1 3.0 4.9 1.8
129 6.4 2.8 5.6 2.1
130 7.2 3.0 5.8 1.6
131 7.4 2.8 6.1 1.9
132 7.9 3.8 6.4 2.0
133 6.4 2.8 5.6 2.2
134 6.3 2.8 5.1 1.5
135 6.1 2.6 5.6 1.4
136 7.7 3.0 6.1 2.3
137 6.3 3.4 5.6 2.4
138 6.4 3.1 5.5 1.8
139 6.0 3.0 4.8 1.8
140 6.9 3.1 5.4 2.1
141 6.7 3.1 5.6 2.4
142 6.9 3.1 5.1 2.3
143 5.8 2.7 5.1 1.9
144 6.8 3.2 5.9 2.3
145 6.7 3.3 5.7 2.5
146 6.7 3.0 5.2 2.3
147 6.3 2.5 5.0 1.9
148 6.5 3.0 5.2 2.0
149 6.2 3.4 5.4 2.3
150 5.9 3.0 5.1 1.8
As you can see it is split based on the classes factor and numeric.

Defining groups in aes() to make same groups having the same color in multiple ggplots

Can somebody try to explain the syntax of aes(color=paste("mean", Role), group=Role) in this question. I really wish to learn how this works. This also works on my data but I do not understand the use of paste() and adding mean
The question was very old but it works on my data.
As #Allan Cameron said in the comments, it takes your variable Role and add the word "mean" before the entries and these will be displayed in the legend because of the colour aesthetic. I will give you a reproducibel example using the iris dataset:
The data:
Sepal.Length Sepal.Width Species
1 5.1 3.5 setosa
2 4.9 3.0 setosa
3 4.7 3.2 setosa
4 4.6 3.1 setosa
5 5.0 3.6 setosa
6 5.4 3.9 setosa
7 4.6 3.4 setosa
8 5.0 3.4 setosa
9 4.4 2.9 setosa
10 4.9 3.1 setosa
11 5.4 3.7 setosa
12 4.8 3.4 setosa
13 4.8 3.0 setosa
14 4.3 3.0 setosa
15 5.8 4.0 setosa
16 5.7 4.4 setosa
17 5.4 3.9 setosa
18 5.1 3.5 setosa
19 5.7 3.8 setosa
20 5.1 3.8 setosa
21 5.4 3.4 setosa
22 5.1 3.7 setosa
23 4.6 3.6 setosa
24 5.1 3.3 setosa
25 4.8 3.4 setosa
26 5.0 3.0 setosa
27 5.0 3.4 setosa
28 5.2 3.5 setosa
29 5.2 3.4 setosa
30 4.7 3.2 setosa
31 4.8 3.1 setosa
32 5.4 3.4 setosa
33 5.2 4.1 setosa
34 5.5 4.2 setosa
35 4.9 3.1 setosa
36 5.0 3.2 setosa
37 5.5 3.5 setosa
38 4.9 3.6 setosa
39 4.4 3.0 setosa
40 5.1 3.4 setosa
41 5.0 3.5 setosa
42 4.5 2.3 setosa
43 4.4 3.2 setosa
44 5.0 3.5 setosa
45 5.1 3.8 setosa
46 4.8 3.0 setosa
47 5.1 3.8 setosa
48 4.6 3.2 setosa
49 5.3 3.7 setosa
50 5.0 3.3 setosa
51 7.0 3.2 versicolor
52 6.4 3.2 versicolor
53 6.9 3.1 versicolor
54 5.5 2.3 versicolor
55 6.5 2.8 versicolor
56 5.7 2.8 versicolor
57 6.3 3.3 versicolor
58 4.9 2.4 versicolor
59 6.6 2.9 versicolor
60 5.2 2.7 versicolor
61 5.0 2.0 versicolor
62 5.9 3.0 versicolor
63 6.0 2.2 versicolor
64 6.1 2.9 versicolor
65 5.6 2.9 versicolor
66 6.7 3.1 versicolor
67 5.6 3.0 versicolor
68 5.8 2.7 versicolor
69 6.2 2.2 versicolor
70 5.6 2.5 versicolor
71 5.9 3.2 versicolor
72 6.1 2.8 versicolor
73 6.3 2.5 versicolor
74 6.1 2.8 versicolor
75 6.4 2.9 versicolor
76 6.6 3.0 versicolor
77 6.8 2.8 versicolor
78 6.7 3.0 versicolor
79 6.0 2.9 versicolor
80 5.7 2.6 versicolor
81 5.5 2.4 versicolor
82 5.5 2.4 versicolor
83 5.8 2.7 versicolor
84 6.0 2.7 versicolor
85 5.4 3.0 versicolor
86 6.0 3.4 versicolor
87 6.7 3.1 versicolor
88 6.3 2.3 versicolor
89 5.6 3.0 versicolor
90 5.5 2.5 versicolor
91 5.5 2.6 versicolor
92 6.1 3.0 versicolor
93 5.8 2.6 versicolor
94 5.0 2.3 versicolor
95 5.6 2.7 versicolor
96 5.7 3.0 versicolor
97 5.7 2.9 versicolor
98 6.2 2.9 versicolor
99 5.1 2.5 versicolor
100 5.7 2.8 versicolor
101 6.3 3.3 virginica
102 5.8 2.7 virginica
103 7.1 3.0 virginica
104 6.3 2.9 virginica
105 6.5 3.0 virginica
106 7.6 3.0 virginica
107 4.9 2.5 virginica
108 7.3 2.9 virginica
109 6.7 2.5 virginica
110 7.2 3.6 virginica
111 6.5 3.2 virginica
112 6.4 2.7 virginica
113 6.8 3.0 virginica
114 5.7 2.5 virginica
115 5.8 2.8 virginica
116 6.4 3.2 virginica
117 6.5 3.0 virginica
118 7.7 3.8 virginica
119 7.7 2.6 virginica
120 6.0 2.2 virginica
121 6.9 3.2 virginica
122 5.6 2.8 virginica
123 7.7 2.8 virginica
124 6.3 2.7 virginica
125 6.7 3.3 virginica
126 7.2 3.2 virginica
127 6.2 2.8 virginica
128 6.1 3.0 virginica
129 6.4 2.8 virginica
130 7.2 3.0 virginica
131 7.4 2.8 virginica
132 7.9 3.8 virginica
133 6.4 2.8 virginica
134 6.3 2.8 virginica
135 6.1 2.6 virginica
136 7.7 3.0 virginica
137 6.3 3.4 virginica
138 6.4 3.1 virginica
139 6.0 3.0 virginica
140 6.9 3.1 virginica
141 6.7 3.1 virginica
142 6.9 3.1 virginica
143 5.8 2.7 virginica
144 6.8 3.2 virginica
145 6.7 3.3 virginica
146 6.7 3.0 virginica
147 6.3 2.5 virginica
148 6.5 3.0 virginica
149 6.2 3.4 virginica
150 5.9 3.0 virginica
You can run the following code:
iris %>%
ggplot(aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_line() +
geom_point() +
stat_summary(fun.y=mean, geom="line", size = 1.5,
linetype="dotted", aes(color=paste("mean", Species)))
Output:
As you can see in the legend of the plot, the species are shown also with "mean" before and those lines are the mean values of those species.

How to exclude particular values when shuffling a column in R?

I would like to shuffle all the rows in a column unless a row contains one particular value, in which case that row should not be included in the shuffling process (i.e. all rows containing the specified value stay where they are).
Using the iris example below, let's say we want to shuffle all rows of the column Species except those rows of Species which contain the value setosa. How do I do that?
Thanks!
library(dplyr)
set.seed(123)
my_df <- iris %>%
mutate(species2 = sample(Species), # shuffles whole column
# attempted conditional shuffle of non-setosa values (does not work!)
species3 = sample(nrow(Species != "setosa")))
We can try making an external function that does that. conditional_shuffle selects cases that don't fit a condition and just uses sample on them:
library(tidyverse)
conditional_shuffle <- function(vec, condition, seed = 123) {
set.seed(seed)
cases2shuffle <- vec != condition
vec[cases2shuffle] <- sample(vec[cases2shuffle])
return(vec)
}
iris %>%
mutate(species2 = conditional_shuffle(Species, "setosa"))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species species2
#> 1 5.1 3.5 1.4 0.2 setosa setosa
#> 2 4.9 3.0 1.4 0.2 setosa setosa
#> 3 4.7 3.2 1.3 0.2 setosa setosa
#> 4 4.6 3.1 1.5 0.2 setosa setosa
#> 5 5.0 3.6 1.4 0.2 setosa setosa
#> 6 5.4 3.9 1.7 0.4 setosa setosa
#> 7 4.6 3.4 1.4 0.3 setosa setosa
#> 8 5.0 3.4 1.5 0.2 setosa setosa
#> 9 4.4 2.9 1.4 0.2 setosa setosa
#> 10 4.9 3.1 1.5 0.1 setosa setosa
#> 11 5.4 3.7 1.5 0.2 setosa setosa
#> 12 4.8 3.4 1.6 0.2 setosa setosa
#> 13 4.8 3.0 1.4 0.1 setosa setosa
#> 14 4.3 3.0 1.1 0.1 setosa setosa
#> 15 5.8 4.0 1.2 0.2 setosa setosa
#> 16 5.7 4.4 1.5 0.4 setosa setosa
#> 17 5.4 3.9 1.3 0.4 setosa setosa
#> 18 5.1 3.5 1.4 0.3 setosa setosa
#> 19 5.7 3.8 1.7 0.3 setosa setosa
#> 20 5.1 3.8 1.5 0.3 setosa setosa
#> 21 5.4 3.4 1.7 0.2 setosa setosa
#> 22 5.1 3.7 1.5 0.4 setosa setosa
#> 23 4.6 3.6 1.0 0.2 setosa setosa
#> 24 5.1 3.3 1.7 0.5 setosa setosa
#> 25 4.8 3.4 1.9 0.2 setosa setosa
#> 26 5.0 3.0 1.6 0.2 setosa setosa
#> 27 5.0 3.4 1.6 0.4 setosa setosa
#> 28 5.2 3.5 1.5 0.2 setosa setosa
#> 29 5.2 3.4 1.4 0.2 setosa setosa
#> 30 4.7 3.2 1.6 0.2 setosa setosa
#> 31 4.8 3.1 1.6 0.2 setosa setosa
#> 32 5.4 3.4 1.5 0.4 setosa setosa
#> 33 5.2 4.1 1.5 0.1 setosa setosa
#> 34 5.5 4.2 1.4 0.2 setosa setosa
#> 35 4.9 3.1 1.5 0.2 setosa setosa
#> 36 5.0 3.2 1.2 0.2 setosa setosa
#> 37 5.5 3.5 1.3 0.2 setosa setosa
#> 38 4.9 3.6 1.4 0.1 setosa setosa
#> 39 4.4 3.0 1.3 0.2 setosa setosa
#> 40 5.1 3.4 1.5 0.2 setosa setosa
#> 41 5.0 3.5 1.3 0.3 setosa setosa
#> 42 4.5 2.3 1.3 0.3 setosa setosa
#> 43 4.4 3.2 1.3 0.2 setosa setosa
#> 44 5.0 3.5 1.6 0.6 setosa setosa
#> 45 5.1 3.8 1.9 0.4 setosa setosa
#> 46 4.8 3.0 1.4 0.3 setosa setosa
#> 47 5.1 3.8 1.6 0.2 setosa setosa
#> 48 4.6 3.2 1.4 0.2 setosa setosa
#> 49 5.3 3.7 1.5 0.2 setosa setosa
#> 50 5.0 3.3 1.4 0.2 setosa setosa
#> 51 7.0 3.2 4.7 1.4 versicolor versicolor
#> 52 6.4 3.2 4.5 1.5 versicolor virginica
#> 53 6.9 3.1 4.9 1.5 versicolor virginica
#> 54 5.5 2.3 4.0 1.3 versicolor versicolor
#> 55 6.5 2.8 4.6 1.5 versicolor virginica
#> 56 5.7 2.8 4.5 1.3 versicolor versicolor
#> 57 6.3 3.3 4.7 1.6 versicolor versicolor
#> 58 4.9 2.4 3.3 1.0 versicolor versicolor
#> 59 6.6 2.9 4.6 1.3 versicolor virginica
#> 60 5.2 2.7 3.9 1.4 versicolor versicolor
#> 61 5.0 2.0 3.5 1.0 versicolor virginica
#> 62 5.9 3.0 4.2 1.5 versicolor virginica
#> 63 6.0 2.2 4.0 1.0 versicolor virginica
#> 64 6.1 2.9 4.7 1.4 versicolor versicolor
#> 65 5.6 2.9 3.6 1.3 versicolor virginica
#> 66 6.7 3.1 4.4 1.4 versicolor versicolor
#> 67 5.6 3.0 4.5 1.5 versicolor versicolor
#> 68 5.8 2.7 4.1 1.0 versicolor virginica
#> 69 6.2 2.2 4.5 1.5 versicolor virginica
#> 70 5.6 2.5 3.9 1.1 versicolor versicolor
#> 71 5.9 3.2 4.8 1.8 versicolor virginica
#> 72 6.1 2.8 4.0 1.3 versicolor virginica
#> 73 6.3 2.5 4.9 1.5 versicolor virginica
#> 74 6.1 2.8 4.7 1.2 versicolor versicolor
#> 75 6.4 2.9 4.3 1.3 versicolor versicolor
#> 76 6.6 3.0 4.4 1.4 versicolor virginica
#> 77 6.8 2.8 4.8 1.4 versicolor virginica
#> 78 6.7 3.0 5.0 1.7 versicolor versicolor
#> 79 6.0 2.9 4.5 1.5 versicolor versicolor
#> 80 5.7 2.6 3.5 1.0 versicolor versicolor
#> 81 5.5 2.4 3.8 1.1 versicolor virginica
#> 82 5.5 2.4 3.7 1.0 versicolor virginica
#> 83 5.8 2.7 3.9 1.2 versicolor virginica
#> 84 6.0 2.7 5.1 1.6 versicolor virginica
#> 85 5.4 3.0 4.5 1.5 versicolor virginica
#> 86 6.0 3.4 4.5 1.6 versicolor versicolor
#> 87 6.7 3.1 4.7 1.5 versicolor virginica
#> 88 6.3 2.3 4.4 1.3 versicolor versicolor
#> 89 5.6 3.0 4.1 1.3 versicolor versicolor
#> 90 5.5 2.5 4.0 1.3 versicolor versicolor
#> 91 5.5 2.6 4.4 1.2 versicolor versicolor
#> 92 6.1 3.0 4.6 1.4 versicolor versicolor
#> 93 5.8 2.6 4.0 1.2 versicolor versicolor
#> 94 5.0 2.3 3.3 1.0 versicolor versicolor
#> 95 5.6 2.7 4.2 1.3 versicolor versicolor
#> 96 5.7 3.0 4.2 1.2 versicolor virginica
#> 97 5.7 2.9 4.2 1.3 versicolor virginica
#> 98 6.2 2.9 4.3 1.3 versicolor virginica
#> 99 5.1 2.5 3.0 1.1 versicolor versicolor
#> 100 5.7 2.8 4.1 1.3 versicolor virginica
#> 101 6.3 3.3 6.0 2.5 virginica virginica
#> 102 5.8 2.7 5.1 1.9 virginica versicolor
#> 103 7.1 3.0 5.9 2.1 virginica virginica
#> 104 6.3 2.9 5.6 1.8 virginica virginica
#> 105 6.5 3.0 5.8 2.2 virginica versicolor
#> 106 7.6 3.0 6.6 2.1 virginica versicolor
#> 107 4.9 2.5 4.5 1.7 virginica versicolor
#> 108 7.3 2.9 6.3 1.8 virginica virginica
#> 109 6.7 2.5 5.8 1.8 virginica virginica
#> 110 7.2 3.6 6.1 2.5 virginica versicolor
#> 111 6.5 3.2 5.1 2.0 virginica virginica
#> 112 6.4 2.7 5.3 1.9 virginica versicolor
#> 113 6.8 3.0 5.5 2.1 virginica versicolor
#> 114 5.7 2.5 5.0 2.0 virginica versicolor
#> 115 5.8 2.8 5.1 2.4 virginica virginica
#> 116 6.4 3.2 5.3 2.3 virginica versicolor
#> 117 6.5 3.0 5.5 1.8 virginica virginica
#> 118 7.7 3.8 6.7 2.2 virginica virginica
#> 119 7.7 2.6 6.9 2.3 virginica versicolor
#> 120 6.0 2.2 5.0 1.5 virginica virginica
#> 121 6.9 3.2 5.7 2.3 virginica versicolor
#> 122 5.6 2.8 4.9 2.0 virginica virginica
#> 123 7.7 2.8 6.7 2.0 virginica versicolor
#> 124 6.3 2.7 4.9 1.8 virginica virginica
#> 125 6.7 3.3 5.7 2.1 virginica virginica
#> 126 7.2 3.2 6.0 1.8 virginica virginica
#> 127 6.2 2.8 4.8 1.8 virginica virginica
#> 128 6.1 3.0 4.9 1.8 virginica versicolor
#> 129 6.4 2.8 5.6 2.1 virginica virginica
#> 130 7.2 3.0 5.8 1.6 virginica versicolor
#> 131 7.4 2.8 6.1 1.9 virginica virginica
#> 132 7.9 3.8 6.4 2.0 virginica versicolor
#> 133 6.4 2.8 5.6 2.2 virginica versicolor
#> 134 6.3 2.8 5.1 1.5 virginica versicolor
#> 135 6.1 2.6 5.6 1.4 virginica virginica
#> 136 7.7 3.0 6.1 2.3 virginica virginica
#> 137 6.3 3.4 5.6 2.4 virginica virginica
#> 138 6.4 3.1 5.5 1.8 virginica virginica
#> 139 6.0 3.0 4.8 1.8 virginica versicolor
#> 140 6.9 3.1 5.4 2.1 virginica versicolor
#> 141 6.7 3.1 5.6 2.4 virginica virginica
#> 142 6.9 3.1 5.1 2.3 virginica versicolor
#> 143 5.8 2.7 5.1 1.9 virginica virginica
#> 144 6.8 3.2 5.9 2.3 virginica versicolor
#> 145 6.7 3.3 5.7 2.5 virginica versicolor
#> 146 6.7 3.0 5.2 2.3 virginica versicolor
#> 147 6.3 2.5 5.0 1.9 virginica virginica
#> 148 6.5 3.0 5.2 2.0 virginica versicolor
#> 149 6.2 3.4 5.4 2.3 virginica virginica
#> 150 5.9 3.0 5.1 1.8 virginica versicolor

Dplyr, Non-standard evaluation and Walrus operator and curly-curly

A real question. Whenever I need to write dplyr functions, I play by the ear.
I am aware of the curly-curly operator which simplifies a lot the task.
https://www.tidyverse.org/blog/2019/06/rlang-0-4-0/
and
https://www.tidyverse.org/blog/2020/02/glue-strings-and-tidy-eval/
What is unclear to me is when to use the simple "=" and the Walrus operator ":=".
For instance consider the snippet at the end of the post.
The functions mean_by and mean_by2 differ only because the former relies on "=" and the latter on ":=", but the result is the same.
However, if I try writing a function which relies on mutate to add a new column, I get an error message if I use "=" instead of ":=" when I create the new column.
Can someone clarify to me why the difference? Does it mean it is safer to use the Walrus operator instead of "="?
Thanks!
library(tidyverse)
mean_by <- function(data, by, var) {
data %>%
group_by({{ by }}) %>%
summarise(avg = mean({{ var }}, na.rm = TRUE))
}
mean_by2 <- function(data, by, var) {
data %>%
group_by({{ by }}) %>%
summarise(avg := mean({{ var }}, na.rm = TRUE))
}
add_new_col <- function(data, old_col, new_col){
data %>%
mutate({{new_col}}:={{old_col}})
}
iris %>% mean_by(Species, Sepal.Width)
#> # A tibble: 3 x 2
#> Species avg
#> <fct> <dbl>
#> 1 setosa 3.43
#> 2 versicolor 2.77
#> 3 virginica 2.97
iris %>% mean_by2(Species, Sepal.Width)
#> # A tibble: 3 x 2
#> Species avg
#> <fct> <dbl>
#> 1 setosa 3.43
#> 2 versicolor 2.77
#> 3 virginica 2.97
iris %>% add_new_col(Species, New_species)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species New_species
#> 1 5.1 3.5 1.4 0.2 setosa setosa
#> 2 4.9 3.0 1.4 0.2 setosa setosa
#> 3 4.7 3.2 1.3 0.2 setosa setosa
#> 4 4.6 3.1 1.5 0.2 setosa setosa
#> 5 5.0 3.6 1.4 0.2 setosa setosa
#> 6 5.4 3.9 1.7 0.4 setosa setosa
#> 7 4.6 3.4 1.4 0.3 setosa setosa
#> 8 5.0 3.4 1.5 0.2 setosa setosa
#> 9 4.4 2.9 1.4 0.2 setosa setosa
#> 10 4.9 3.1 1.5 0.1 setosa setosa
#> 11 5.4 3.7 1.5 0.2 setosa setosa
#> 12 4.8 3.4 1.6 0.2 setosa setosa
#> 13 4.8 3.0 1.4 0.1 setosa setosa
#> 14 4.3 3.0 1.1 0.1 setosa setosa
#> 15 5.8 4.0 1.2 0.2 setosa setosa
#> 16 5.7 4.4 1.5 0.4 setosa setosa
#> 17 5.4 3.9 1.3 0.4 setosa setosa
#> 18 5.1 3.5 1.4 0.3 setosa setosa
#> 19 5.7 3.8 1.7 0.3 setosa setosa
#> 20 5.1 3.8 1.5 0.3 setosa setosa
#> 21 5.4 3.4 1.7 0.2 setosa setosa
#> 22 5.1 3.7 1.5 0.4 setosa setosa
#> 23 4.6 3.6 1.0 0.2 setosa setosa
#> 24 5.1 3.3 1.7 0.5 setosa setosa
#> 25 4.8 3.4 1.9 0.2 setosa setosa
#> 26 5.0 3.0 1.6 0.2 setosa setosa
#> 27 5.0 3.4 1.6 0.4 setosa setosa
#> 28 5.2 3.5 1.5 0.2 setosa setosa
#> 29 5.2 3.4 1.4 0.2 setosa setosa
#> 30 4.7 3.2 1.6 0.2 setosa setosa
#> 31 4.8 3.1 1.6 0.2 setosa setosa
#> 32 5.4 3.4 1.5 0.4 setosa setosa
#> 33 5.2 4.1 1.5 0.1 setosa setosa
#> 34 5.5 4.2 1.4 0.2 setosa setosa
#> 35 4.9 3.1 1.5 0.2 setosa setosa
#> 36 5.0 3.2 1.2 0.2 setosa setosa
#> 37 5.5 3.5 1.3 0.2 setosa setosa
#> 38 4.9 3.6 1.4 0.1 setosa setosa
#> 39 4.4 3.0 1.3 0.2 setosa setosa
#> 40 5.1 3.4 1.5 0.2 setosa setosa
#> 41 5.0 3.5 1.3 0.3 setosa setosa
#> 42 4.5 2.3 1.3 0.3 setosa setosa
#> 43 4.4 3.2 1.3 0.2 setosa setosa
#> 44 5.0 3.5 1.6 0.6 setosa setosa
#> 45 5.1 3.8 1.9 0.4 setosa setosa
#> 46 4.8 3.0 1.4 0.3 setosa setosa
#> 47 5.1 3.8 1.6 0.2 setosa setosa
#> 48 4.6 3.2 1.4 0.2 setosa setosa
#> 49 5.3 3.7 1.5 0.2 setosa setosa
#> 50 5.0 3.3 1.4 0.2 setosa setosa
#> 51 7.0 3.2 4.7 1.4 versicolor versicolor
#> 52 6.4 3.2 4.5 1.5 versicolor versicolor
#> 53 6.9 3.1 4.9 1.5 versicolor versicolor
#> 54 5.5 2.3 4.0 1.3 versicolor versicolor
#> 55 6.5 2.8 4.6 1.5 versicolor versicolor
#> 56 5.7 2.8 4.5 1.3 versicolor versicolor
#> 57 6.3 3.3 4.7 1.6 versicolor versicolor
#> 58 4.9 2.4 3.3 1.0 versicolor versicolor
#> 59 6.6 2.9 4.6 1.3 versicolor versicolor
#> 60 5.2 2.7 3.9 1.4 versicolor versicolor
#> 61 5.0 2.0 3.5 1.0 versicolor versicolor
#> 62 5.9 3.0 4.2 1.5 versicolor versicolor
#> 63 6.0 2.2 4.0 1.0 versicolor versicolor
#> 64 6.1 2.9 4.7 1.4 versicolor versicolor
#> 65 5.6 2.9 3.6 1.3 versicolor versicolor
#> 66 6.7 3.1 4.4 1.4 versicolor versicolor
#> 67 5.6 3.0 4.5 1.5 versicolor versicolor
#> 68 5.8 2.7 4.1 1.0 versicolor versicolor
#> 69 6.2 2.2 4.5 1.5 versicolor versicolor
#> 70 5.6 2.5 3.9 1.1 versicolor versicolor
#> 71 5.9 3.2 4.8 1.8 versicolor versicolor
#> 72 6.1 2.8 4.0 1.3 versicolor versicolor
#> 73 6.3 2.5 4.9 1.5 versicolor versicolor
#> 74 6.1 2.8 4.7 1.2 versicolor versicolor
#> 75 6.4 2.9 4.3 1.3 versicolor versicolor
#> 76 6.6 3.0 4.4 1.4 versicolor versicolor
#> 77 6.8 2.8 4.8 1.4 versicolor versicolor
#> 78 6.7 3.0 5.0 1.7 versicolor versicolor
#> 79 6.0 2.9 4.5 1.5 versicolor versicolor
#> 80 5.7 2.6 3.5 1.0 versicolor versicolor
#> 81 5.5 2.4 3.8 1.1 versicolor versicolor
#> 82 5.5 2.4 3.7 1.0 versicolor versicolor
#> 83 5.8 2.7 3.9 1.2 versicolor versicolor
#> 84 6.0 2.7 5.1 1.6 versicolor versicolor
#> 85 5.4 3.0 4.5 1.5 versicolor versicolor
#> 86 6.0 3.4 4.5 1.6 versicolor versicolor
#> 87 6.7 3.1 4.7 1.5 versicolor versicolor
#> 88 6.3 2.3 4.4 1.3 versicolor versicolor
#> 89 5.6 3.0 4.1 1.3 versicolor versicolor
#> 90 5.5 2.5 4.0 1.3 versicolor versicolor
#> 91 5.5 2.6 4.4 1.2 versicolor versicolor
#> 92 6.1 3.0 4.6 1.4 versicolor versicolor
#> 93 5.8 2.6 4.0 1.2 versicolor versicolor
#> 94 5.0 2.3 3.3 1.0 versicolor versicolor
#> 95 5.6 2.7 4.2 1.3 versicolor versicolor
#> 96 5.7 3.0 4.2 1.2 versicolor versicolor
#> 97 5.7 2.9 4.2 1.3 versicolor versicolor
#> 98 6.2 2.9 4.3 1.3 versicolor versicolor
#> 99 5.1 2.5 3.0 1.1 versicolor versicolor
#> 100 5.7 2.8 4.1 1.3 versicolor versicolor
#> 101 6.3 3.3 6.0 2.5 virginica virginica
#> 102 5.8 2.7 5.1 1.9 virginica virginica
#> 103 7.1 3.0 5.9 2.1 virginica virginica
#> 104 6.3 2.9 5.6 1.8 virginica virginica
#> 105 6.5 3.0 5.8 2.2 virginica virginica
#> 106 7.6 3.0 6.6 2.1 virginica virginica
#> 107 4.9 2.5 4.5 1.7 virginica virginica
#> 108 7.3 2.9 6.3 1.8 virginica virginica
#> 109 6.7 2.5 5.8 1.8 virginica virginica
#> 110 7.2 3.6 6.1 2.5 virginica virginica
#> 111 6.5 3.2 5.1 2.0 virginica virginica
#> 112 6.4 2.7 5.3 1.9 virginica virginica
#> 113 6.8 3.0 5.5 2.1 virginica virginica
#> 114 5.7 2.5 5.0 2.0 virginica virginica
#> 115 5.8 2.8 5.1 2.4 virginica virginica
#> 116 6.4 3.2 5.3 2.3 virginica virginica
#> 117 6.5 3.0 5.5 1.8 virginica virginica
#> 118 7.7 3.8 6.7 2.2 virginica virginica
#> 119 7.7 2.6 6.9 2.3 virginica virginica
#> 120 6.0 2.2 5.0 1.5 virginica virginica
#> 121 6.9 3.2 5.7 2.3 virginica virginica
#> 122 5.6 2.8 4.9 2.0 virginica virginica
#> 123 7.7 2.8 6.7 2.0 virginica virginica
#> 124 6.3 2.7 4.9 1.8 virginica virginica
#> 125 6.7 3.3 5.7 2.1 virginica virginica
#> 126 7.2 3.2 6.0 1.8 virginica virginica
#> 127 6.2 2.8 4.8 1.8 virginica virginica
#> 128 6.1 3.0 4.9 1.8 virginica virginica
#> 129 6.4 2.8 5.6 2.1 virginica virginica
#> 130 7.2 3.0 5.8 1.6 virginica virginica
#> 131 7.4 2.8 6.1 1.9 virginica virginica
#> 132 7.9 3.8 6.4 2.0 virginica virginica
#> 133 6.4 2.8 5.6 2.2 virginica virginica
#> 134 6.3 2.8 5.1 1.5 virginica virginica
#> 135 6.1 2.6 5.6 1.4 virginica virginica
#> 136 7.7 3.0 6.1 2.3 virginica virginica
#> 137 6.3 3.4 5.6 2.4 virginica virginica
#> 138 6.4 3.1 5.5 1.8 virginica virginica
#> 139 6.0 3.0 4.8 1.8 virginica virginica
#> 140 6.9 3.1 5.4 2.1 virginica virginica
#> 141 6.7 3.1 5.6 2.4 virginica virginica
#> 142 6.9 3.1 5.1 2.3 virginica virginica
#> 143 5.8 2.7 5.1 1.9 virginica virginica
#> 144 6.8 3.2 5.9 2.3 virginica virginica
#> 145 6.7 3.3 5.7 2.5 virginica virginica
#> 146 6.7 3.0 5.2 2.3 virginica virginica
#> 147 6.3 2.5 5.0 1.9 virginica virginica
#> 148 6.5 3.0 5.2 2.0 virginica virginica
#> 149 6.2 3.4 5.4 2.3 virginica virginica
#> 150 5.9 3.0 5.1 1.8 virginica virginica
Created on 2020-04-18 by the reprex package (v0.3.0)
The rule of thumb is simple: if you are using any form of quasiquation (i.e., !! and {{ operators) on the left-hand side of the assignment, you need the walrus operator.
s = sym("abc")
## Quasi-quotation on the left-hand of the assignment
iris %>% mutate( !!s = Petal.Length * Petal.Width ) # Error: unexpected '='
## Using walrus fixes the issue
iris %>% mutate( !!s := Petal.Length * Petal.Width ) # Works, creates column abc
## No quasi-quotation on the left-hand side, so = is enough
iris %>% mutate( s = Petal.Length * Petal.Width ) # Also works, creates column s
Note that the walrus operator only works in functions that support quasiquotation. It is not supported in the general case:
list( a := 5 ) ## Error
rlang::list2( a := 5 ) ## Works
In the second case, we are creating a column named 'avg' and it is not the one provided by the user as unquoted argument. So, we can use = while in the last case, it is an argument that is provided in the function and here we use {{}} to do the evaluation

Resources