Column mean of data.frame (list) in R - r

Data
Please, I need to calculate the mean of the column "Sepal.Length" for the specie virginica in this data.frame:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
11 5.4 3.7 1.5 0.2 setosa
12 4.8 3.4 1.6 0.2 setosa
13 4.8 3.0 1.4 0.1 setosa
14 4.3 3.0 1.1 0.1 setosa
15 5.8 4.0 1.2 0.2 setosa
16 5.7 4.4 1.5 0.4 setosa
17 5.4 3.9 1.3 0.4 setosa
18 5.1 3.5 1.4 0.3 setosa
19 5.7 3.8 1.7 0.3 setosa
20 5.1 3.8 1.5 0.3 setosa
21 5.4 3.4 1.7 0.2 setosa
22 5.1 3.7 1.5 0.4 setosa
23 4.6 3.6 1.0 0.2 setosa
24 5.1 3.3 1.7 0.5 setosa
25 4.8 3.4 1.9 0.2 setosa
26 5.0 3.0 1.6 0.2 setosa
27 5.0 3.4 1.6 0.4 setosa
28 5.2 3.5 1.5 0.2 setosa
29 5.2 3.4 1.4 0.2 setosa
30 4.7 3.2 1.6 0.2 setosa
31 4.8 3.1 1.6 0.2 setosa
32 5.4 3.4 1.5 0.4 setosa
33 5.2 4.1 1.5 0.1 setosa
34 5.5 4.2 1.4 0.2 setosa
35 4.9 3.1 1.5 0.2 setosa
36 5.0 3.2 1.2 0.2 setosa
37 5.5 3.5 1.3 0.2 setosa
38 4.9 3.6 1.4 0.1 setosa
39 4.4 3.0 1.3 0.2 setosa
40 5.1 3.4 1.5 0.2 setosa
41 5.0 3.5 1.3 0.3 setosa
42 4.5 2.3 1.3 0.3 setosa
43 4.4 3.2 1.3 0.2 setosa
44 5.0 3.5 1.6 0.6 setosa
45 5.1 3.8 1.9 0.4 setosa
46 4.8 3.0 1.4 0.3 setosa
47 5.1 3.8 1.6 0.2 setosa
48 4.6 3.2 1.4 0.2 setosa
49 5.3 3.7 1.5 0.2 setosa
50 5.0 3.3 1.4 0.2 setosa
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
53 6.9 3.1 4.9 1.5 versicolor
54 5.5 2.3 4.0 1.3 versicolor
55 6.5 2.8 4.6 1.5 versicolor
56 5.7 2.8 4.5 1.3 versicolor
57 6.3 3.3 4.7 1.6 versicolor
58 4.9 2.4 3.3 1.0 versicolor
59 6.6 2.9 4.6 1.3 versicolor
60 5.2 2.7 3.9 1.4 versicolor
61 5.0 2.0 3.5 1.0 versicolor
62 5.9 3.0 4.2 1.5 versicolor
63 6.0 2.2 4.0 1.0 versicolor
64 6.1 2.9 4.7 1.4 versicolor
65 5.6 2.9 3.6 1.3 versicolor
66 6.7 3.1 4.4 1.4 versicolor
67 5.6 3.0 4.5 1.5 versicolor
68 5.8 2.7 4.1 1.0 versicolor
69 6.2 2.2 4.5 1.5 versicolor
70 5.6 2.5 3.9 1.1 versicolor
71 5.9 3.2 4.8 1.8 versicolor
72 6.1 2.8 4.0 1.3 versicolor
73 6.3 2.5 4.9 1.5 versicolor
74 6.1 2.8 4.7 1.2 versicolor
75 6.4 2.9 4.3 1.3 versicolor
76 6.6 3.0 4.4 1.4 versicolor
77 6.8 2.8 4.8 1.4 versicolor
78 6.7 3.0 5.0 1.7 versicolor
79 6.0 2.9 4.5 1.5 versicolor
80 5.7 2.6 3.5 1.0 versicolor
81 5.5 2.4 3.8 1.1 versicolor
82 5.5 2.4 3.7 1.0 versicolor
83 5.8 2.7 3.9 1.2 versicolor
84 6.0 2.7 5.1 1.6 versicolor
85 5.4 3.0 4.5 1.5 versicolor
86 6.0 3.4 4.5 1.6 versicolor
87 6.7 3.1 4.7 1.5 versicolor
88 6.3 2.3 4.4 1.3 versicolor
89 5.6 3.0 4.1 1.3 versicolor
90 5.5 2.5 4.0 1.3 versicolor
91 5.5 2.6 4.4 1.2 versicolor
92 6.1 3.0 4.6 1.4 versicolor
93 5.8 2.6 4.0 1.2 versicolor
94 5.0 2.3 3.3 1.0 versicolor
95 5.6 2.7 4.2 1.3 versicolor
96 5.7 3.0 4.2 1.2 versicolor
97 5.7 2.9 4.2 1.3 versicolor
98 6.2 2.9 4.3 1.3 versicolor
99 5.1 2.5 3.0 1.1 versicolor
100 5.7 2.8 4.1 1.3 versicolor
101 6.3 3.3 6.0 2.5 virginica
102 5.8 2.7 5.1 1.9 virginica
103 7.1 3.0 5.9 2.1 virginica
104 6.3 2.9 5.6 1.8 virginica
105 6.5 3.0 5.8 2.2 virginica
106 7.6 3.0 6.6 2.1 virginica
107 4.9 2.5 4.5 1.7 virginica
108 7.3 2.9 6.3 1.8 virginica
109 6.7 2.5 5.8 1.8 virginica
110 7.2 3.6 6.1 2.5 virginica
111 6.5 3.2 5.1 2.0 virginica
112 6.4 2.7 5.3 1.9 virginica
113 6.8 3.0 5.5 2.1 virginica
114 5.7 2.5 5.0 2.0 virginica
115 5.8 2.8 5.1 2.4 virginica
116 6.4 3.2 5.3 2.3 virginica
117 6.5 3.0 5.5 1.8 virginica
118 7.7 3.8 6.7 2.2 virginica
119 7.7 2.6 6.9 2.3 virginica
120 6.0 2.2 5.0 1.5 virginica
121 6.9 3.2 5.7 2.3 virginica
122 5.6 2.8 4.9 2.0 virginica
123 7.7 2.8 6.7 2.0 virginica
124 6.3 2.7 4.9 1.8 virginica
125 6.7 3.3 5.7 2.1 virginica
126 7.2 3.2 6.0 1.8 virginica
127 6.2 2.8 4.8 1.8 virginica
128 6.1 3.0 4.9 1.8 virginica
129 6.4 2.8 5.6 2.1 virginica
130 7.2 3.0 5.8 1.6 virginica
131 7.4 2.8 6.1 1.9 virginica
132 7.9 3.8 6.4 2.0 virginica
133 6.4 2.8 5.6 2.2 virginica
134 6.3 2.8 5.1 1.5 virginica
135 6.1 2.6 5.6 1.4 virginica
136 7.7 3.0 6.1 2.3 virginica
137 6.3 3.4 5.6 2.4 virginica
138 6.4 3.1 5.5 1.8 virginica
139 6.0 3.0 4.8 1.8 virginica
140 6.9 3.1 5.4 2.1 virginica
141 6.7 3.1 5.6 2.4 virginica
142 6.9 3.1 5.1 2.3 virginica
143 5.8 2.7 5.1 1.9 virginica
144 6.8 3.2 5.9 2.3 virginica
145 6.7 3.3 5.7 2.5 virginica
146 6.7 3.0 5.2 2.3 virginica
147 6.3 2.5 5.0 1.9 virginica
148 6.5 3.0 5.2 2.0 virginica
149 6.2 3.4 5.4 2.3 virginica
150 5.9 3.0 5.1 1.8 virginica
When I used sapply(s, function(x) colMeans(x[, c("virginica")], na.rm =TRUE))
I get this error:
Error in `[.data.frame`(x, , c("virginica")) : undefined columns selected
When I use sapply(split(iris[1:2],iris$Specie),mean, na.rm = TRUE)
I get this response:
setosa versicolor virginica
NA NA NA
Mensajes de aviso perdidos
1: In mean.default(X[[1L]], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(X[[2L]], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(X[[3L]], ...) :
argument is not numeric or logical: returning NA
AND
When I use apply(iris,2,mean)
I get this:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
NA NA NA NA NA
Mensajes de aviso perdidos
1: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
4: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
5: In mean.default(newX[, i], ...) :
argument is not numeric or logical: returning NA
I am new in programming.
Thanks in advance!!

In order to correct the errors, you could use:
colMeans(subset(s, Species=="virginica", select=-Species), na.rm=TRUE)
#Sepal.Length Sepal.Width Petal.Length Petal.Width
# 6.588 2.974 5.552 2.026
For the second case:
sapply(split(iris[1:2], iris$Species), colMeans, na.rm=TRUE)
# setosa versicolor virginica
#Sepal.Length 5.006 5.936 6.588
#Sepal.Width 3.428 2.770 2.974
mean would not work the same way as min, max in a data.frame
dat <- data.frame(col1=1:5, col2=6:10)
mean(dat)
#[1] NA
Also, your title indicates you need column means, So, it is better to use colMeans. But, if you are looking for a single value from all the columns just like min
min(dat)
#[1] 1
mean(colMeans(dat))
#[1] 5.5
There are several methods to get the mean by group. If you have several columns,
library(dplyr)
s%>%
group_by(Species) %>%
summarise_each(funs(mean=mean(., na.rm=TRUE)))
#Source: local data frame [3 x 5]
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#1 setosa 5.006 3.428 1.462 0.246
#2 versicolor 5.936 2.770 4.260 1.326
#3 virginica 6.588 2.974 5.552 2.026
Or
library(data.table)
setDT(s)[, lapply(.SD, mean, na.rm=TRUE),by='Species']
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#1: setosa 5.006 3.428 1.462 0.246
#2: versicolor 5.936 2.770 4.260 1.326
#3: virginica 6.588 2.974 5.552 2.026

Try:
mean( iris[iris$Species=='virginica', ]$Sepal.Length )
[1] 6.588
or:
mean(iris[iris$Species=='virginica',1])
[1] 6.588
For mean of all species:
with(iris, tapply(Sepal.Length, Species, mean))
setosa versicolor virginica
5.006 5.936 6.588
For data.frame output:
with(iris, aggregate(Sepal.Length~Species, FUN=mean))
Species Sepal.Length
1 setosa 5.006
2 versicolor 5.936
3 virginica 6.588
For this species vs others:
by(iris[,1], iris$Species=='virginica', mean)
iris$Species == "virginica": FALSE
[1] 5.471
-------------------------------------------------------------------------------------------------
iris$Species == "virginica": TRUE
[1] 6.588

How about using by()?
### Get mean for the 1st-4th column for each level of Species
by(iris[,1:4], iris$Species, colMeans)

Related

How to order a string column by a concrete order [duplicate]

This question already has answers here:
Arranging rows in custom order using dplyr
(3 answers)
Reorder rows using custom order
(2 answers)
Arrange rows in custom order using R [duplicate]
(4 answers)
Order data frame rows according to vector with specific order
(6 answers)
Closed last month.
For example, I have the typical dataframe:
library(tidyverse)
my_data <- as_tibble(iris)
my_data
# A tibble: 150 × 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <fct>
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
# … with 140 more rows
I just want to reorder the dataset by the column "Species" (which has 3 values: setosa, virginica and versicolor), specifying an exact order of rows. For example: virginica, then setosa, then versicolor.
You can use arrange and match:
library(dplyr)
iris %>%
arrange(match(Species, c("virginica", "setosa", "versicolor")))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 6.3 3.3 6.0 2.5 virginica
#> 2 5.8 2.7 5.1 1.9 virginica
#> 3 7.1 3.0 5.9 2.1 virginica
#> 4 6.3 2.9 5.6 1.8 virginica
#> 5 6.5 3.0 5.8 2.2 virginica
#> 6 7.6 3.0 6.6 2.1 virginica
#> 7 4.9 2.5 4.5 1.7 virginica
#> 8 7.3 2.9 6.3 1.8 virginica
#> 9 6.7 2.5 5.8 1.8 virginica
#> 10 7.2 3.6 6.1 2.5 virginica
#> 11 6.5 3.2 5.1 2.0 virginica
#> 12 6.4 2.7 5.3 1.9 virginica
#> 13 6.8 3.0 5.5 2.1 virginica
#> 14 5.7 2.5 5.0 2.0 virginica
#> 15 5.8 2.8 5.1 2.4 virginica
#> 16 6.4 3.2 5.3 2.3 virginica
#> 17 6.5 3.0 5.5 1.8 virginica
#> 18 7.7 3.8 6.7 2.2 virginica
#> 19 7.7 2.6 6.9 2.3 virginica
#> 20 6.0 2.2 5.0 1.5 virginica
#> 21 6.9 3.2 5.7 2.3 virginica
#> 22 5.6 2.8 4.9 2.0 virginica
#> 23 7.7 2.8 6.7 2.0 virginica
#> 24 6.3 2.7 4.9 1.8 virginica
#> 25 6.7 3.3 5.7 2.1 virginica
#> 26 7.2 3.2 6.0 1.8 virginica
#> 27 6.2 2.8 4.8 1.8 virginica
#> 28 6.1 3.0 4.9 1.8 virginica
#> 29 6.4 2.8 5.6 2.1 virginica
#> 30 7.2 3.0 5.8 1.6 virginica
#> 31 7.4 2.8 6.1 1.9 virginica
#> 32 7.9 3.8 6.4 2.0 virginica
#> 33 6.4 2.8 5.6 2.2 virginica
#> 34 6.3 2.8 5.1 1.5 virginica
#> 35 6.1 2.6 5.6 1.4 virginica
#> 36 7.7 3.0 6.1 2.3 virginica
#> 37 6.3 3.4 5.6 2.4 virginica
#> 38 6.4 3.1 5.5 1.8 virginica
#> 39 6.0 3.0 4.8 1.8 virginica
#> 40 6.9 3.1 5.4 2.1 virginica
#> 41 6.7 3.1 5.6 2.4 virginica
#> 42 6.9 3.1 5.1 2.3 virginica
#> 43 5.8 2.7 5.1 1.9 virginica
#> 44 6.8 3.2 5.9 2.3 virginica
#> 45 6.7 3.3 5.7 2.5 virginica
#> 46 6.7 3.0 5.2 2.3 virginica
#> 47 6.3 2.5 5.0 1.9 virginica
#> 48 6.5 3.0 5.2 2.0 virginica
#> 49 6.2 3.4 5.4 2.3 virginica
#> 50 5.9 3.0 5.1 1.8 virginica
#> 51 5.1 3.5 1.4 0.2 setosa
#> 52 4.9 3.0 1.4 0.2 setosa
#> 53 4.7 3.2 1.3 0.2 setosa
#> 54 4.6 3.1 1.5 0.2 setosa
#> 55 5.0 3.6 1.4 0.2 setosa
#> 56 5.4 3.9 1.7 0.4 setosa
#> 57 4.6 3.4 1.4 0.3 setosa
#> 58 5.0 3.4 1.5 0.2 setosa
#> 59 4.4 2.9 1.4 0.2 setosa
#> 60 4.9 3.1 1.5 0.1 setosa
#> 61 5.4 3.7 1.5 0.2 setosa
#> 62 4.8 3.4 1.6 0.2 setosa
#> 63 4.8 3.0 1.4 0.1 setosa
#> 64 4.3 3.0 1.1 0.1 setosa
#> 65 5.8 4.0 1.2 0.2 setosa
#> 66 5.7 4.4 1.5 0.4 setosa
#> 67 5.4 3.9 1.3 0.4 setosa
#> 68 5.1 3.5 1.4 0.3 setosa
#> 69 5.7 3.8 1.7 0.3 setosa
#> 70 5.1 3.8 1.5 0.3 setosa
#> 71 5.4 3.4 1.7 0.2 setosa
#> 72 5.1 3.7 1.5 0.4 setosa
#> 73 4.6 3.6 1.0 0.2 setosa
#> 74 5.1 3.3 1.7 0.5 setosa
#> 75 4.8 3.4 1.9 0.2 setosa
#> 76 5.0 3.0 1.6 0.2 setosa
#> 77 5.0 3.4 1.6 0.4 setosa
#> 78 5.2 3.5 1.5 0.2 setosa
#> 79 5.2 3.4 1.4 0.2 setosa
#> 80 4.7 3.2 1.6 0.2 setosa
#> 81 4.8 3.1 1.6 0.2 setosa
#> 82 5.4 3.4 1.5 0.4 setosa
#> 83 5.2 4.1 1.5 0.1 setosa
#> 84 5.5 4.2 1.4 0.2 setosa
#> 85 4.9 3.1 1.5 0.2 setosa
#> 86 5.0 3.2 1.2 0.2 setosa
#> 87 5.5 3.5 1.3 0.2 setosa
#> 88 4.9 3.6 1.4 0.1 setosa
#> 89 4.4 3.0 1.3 0.2 setosa
#> 90 5.1 3.4 1.5 0.2 setosa
#> 91 5.0 3.5 1.3 0.3 setosa
#> 92 4.5 2.3 1.3 0.3 setosa
#> 93 4.4 3.2 1.3 0.2 setosa
#> 94 5.0 3.5 1.6 0.6 setosa
#> 95 5.1 3.8 1.9 0.4 setosa
#> 96 4.8 3.0 1.4 0.3 setosa
#> 97 5.1 3.8 1.6 0.2 setosa
#> 98 4.6 3.2 1.4 0.2 setosa
#> 99 5.3 3.7 1.5 0.2 setosa
#> 100 5.0 3.3 1.4 0.2 setosa
#> 101 7.0 3.2 4.7 1.4 versicolor
#> 102 6.4 3.2 4.5 1.5 versicolor
#> 103 6.9 3.1 4.9 1.5 versicolor
#> 104 5.5 2.3 4.0 1.3 versicolor
#> 105 6.5 2.8 4.6 1.5 versicolor
#> 106 5.7 2.8 4.5 1.3 versicolor
#> 107 6.3 3.3 4.7 1.6 versicolor
#> 108 4.9 2.4 3.3 1.0 versicolor
#> 109 6.6 2.9 4.6 1.3 versicolor
#> 110 5.2 2.7 3.9 1.4 versicolor
#> 111 5.0 2.0 3.5 1.0 versicolor
#> 112 5.9 3.0 4.2 1.5 versicolor
#> 113 6.0 2.2 4.0 1.0 versicolor
#> 114 6.1 2.9 4.7 1.4 versicolor
#> 115 5.6 2.9 3.6 1.3 versicolor
#> 116 6.7 3.1 4.4 1.4 versicolor
#> 117 5.6 3.0 4.5 1.5 versicolor
#> 118 5.8 2.7 4.1 1.0 versicolor
#> 119 6.2 2.2 4.5 1.5 versicolor
#> 120 5.6 2.5 3.9 1.1 versicolor
#> 121 5.9 3.2 4.8 1.8 versicolor
#> 122 6.1 2.8 4.0 1.3 versicolor
#> 123 6.3 2.5 4.9 1.5 versicolor
#> 124 6.1 2.8 4.7 1.2 versicolor
#> 125 6.4 2.9 4.3 1.3 versicolor
#> 126 6.6 3.0 4.4 1.4 versicolor
#> 127 6.8 2.8 4.8 1.4 versicolor
#> 128 6.7 3.0 5.0 1.7 versicolor
#> 129 6.0 2.9 4.5 1.5 versicolor
#> 130 5.7 2.6 3.5 1.0 versicolor
#> 131 5.5 2.4 3.8 1.1 versicolor
#> 132 5.5 2.4 3.7 1.0 versicolor
#> 133 5.8 2.7 3.9 1.2 versicolor
#> 134 6.0 2.7 5.1 1.6 versicolor
#> 135 5.4 3.0 4.5 1.5 versicolor
#> 136 6.0 3.4 4.5 1.6 versicolor
#> 137 6.7 3.1 4.7 1.5 versicolor
#> 138 6.3 2.3 4.4 1.3 versicolor
#> 139 5.6 3.0 4.1 1.3 versicolor
#> 140 5.5 2.5 4.0 1.3 versicolor
#> 141 5.5 2.6 4.4 1.2 versicolor
#> 142 6.1 3.0 4.6 1.4 versicolor
#> 143 5.8 2.6 4.0 1.2 versicolor
#> 144 5.0 2.3 3.3 1.0 versicolor
#> 145 5.6 2.7 4.2 1.3 versicolor
#> 146 5.7 3.0 4.2 1.2 versicolor
#> 147 5.7 2.9 4.2 1.3 versicolor
#> 148 6.2 2.9 4.3 1.3 versicolor
#> 149 5.1 2.5 3.0 1.1 versicolor
#> 150 5.7 2.8 4.1 1.3 versicolor

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.

Identify rows based on 2 external vectors

I'm trying to flag rows that have values that match elements in 2 external vectors. I need these values to match, meaning it can't be any case, but something where both conditions are met (i.e. not using an %in% operator).
Here's what I've tried:
widths <- c(1.2, 1.7, 1.8, 1.8, 1.9)
species <- c(rep("versicolor", 3), rep("virginica", 2))
iris_flagged <- iris %>%
filter(Species != "setosa") %>%
mutate(flag = ifelse((Petal.Width == widths & Species == species), "check", "")) %>%
arrange(Species, Petal.Width) #to help with visualization
But when I run this some flags are missed. If you look at the data you'll see that rows 14, 50, 57, 59, 61:64, and 66:71 are missing flags.
A clunky, non-robust method could look like this, but I really want to make it robust and use these vectors to my advantage.
iris_flagged2 <- iris %>%
filter(Species != "setosa") %>%
mutate(flag = ifelse((Species == "versicolor" & Petal.Width == 1.2 |
Species == "versicolor" & Petal.Width == 1.7 |
Species == "versicolor" & Petal.Width == 1.8 |
Species == "virginica" & Petal.Width == 1.8 |
Species == "virginica" & Petal.Width == 1.9),
"check", "")) %>%
arrange(Species, Petal.Width)
Thanks for your help. I tried to search but wasn't sure how to phrase my question.
Here is one way using map2 to pass each pair of condition and reduce to combine them into one.
library(tidyverse)
iris2 <- iris %>% filter(Species != "setosa")
iris2 <- iris2 %>%
mutate(flag = ifelse(map2(species, widths,
~Species == .x & Petal.Width == .y) %>% reduce(`|`), 'check', ''))
iris2
Additional to the comment of #akrun, here is a possible solution for your task:
library(dplyr)
iris %>%
mutate(across(-Species, ~round(.,1)),
flag = ifelse(Petal.Width %in% widths &
Species %in% species, "check",""))
output:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species flag
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
11 5.4 3.7 1.5 0.2 setosa
12 4.8 3.4 1.6 0.2 setosa
13 4.8 3.0 1.4 0.1 setosa
14 4.3 3.0 1.1 0.1 setosa
15 5.8 4.0 1.2 0.2 setosa
16 5.7 4.4 1.5 0.4 setosa
17 5.4 3.9 1.3 0.4 setosa
18 5.1 3.5 1.4 0.3 setosa
19 5.7 3.8 1.7 0.3 setosa
20 5.1 3.8 1.5 0.3 setosa
21 5.4 3.4 1.7 0.2 setosa
22 5.1 3.7 1.5 0.4 setosa
23 4.6 3.6 1.0 0.2 setosa
24 5.1 3.3 1.7 0.5 setosa
25 4.8 3.4 1.9 0.2 setosa
26 5.0 3.0 1.6 0.2 setosa
27 5.0 3.4 1.6 0.4 setosa
28 5.2 3.5 1.5 0.2 setosa
29 5.2 3.4 1.4 0.2 setosa
30 4.7 3.2 1.6 0.2 setosa
31 4.8 3.1 1.6 0.2 setosa
32 5.4 3.4 1.5 0.4 setosa
33 5.2 4.1 1.5 0.1 setosa
34 5.5 4.2 1.4 0.2 setosa
35 4.9 3.1 1.5 0.2 setosa
36 5.0 3.2 1.2 0.2 setosa
37 5.5 3.5 1.3 0.2 setosa
38 4.9 3.6 1.4 0.1 setosa
39 4.4 3.0 1.3 0.2 setosa
40 5.1 3.4 1.5 0.2 setosa
41 5.0 3.5 1.3 0.3 setosa
42 4.5 2.3 1.3 0.3 setosa
43 4.4 3.2 1.3 0.2 setosa
44 5.0 3.5 1.6 0.6 setosa
45 5.1 3.8 1.9 0.4 setosa
46 4.8 3.0 1.4 0.3 setosa
47 5.1 3.8 1.6 0.2 setosa
48 4.6 3.2 1.4 0.2 setosa
49 5.3 3.7 1.5 0.2 setosa
50 5.0 3.3 1.4 0.2 setosa
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
53 6.9 3.1 4.9 1.5 versicolor
54 5.5 2.3 4.0 1.3 versicolor
55 6.5 2.8 4.6 1.5 versicolor
56 5.7 2.8 4.5 1.3 versicolor
57 6.3 3.3 4.7 1.6 versicolor
58 4.9 2.4 3.3 1.0 versicolor
59 6.6 2.9 4.6 1.3 versicolor
60 5.2 2.7 3.9 1.4 versicolor
61 5.0 2.0 3.5 1.0 versicolor
62 5.9 3.0 4.2 1.5 versicolor
63 6.0 2.2 4.0 1.0 versicolor
64 6.1 2.9 4.7 1.4 versicolor
65 5.6 2.9 3.6 1.3 versicolor
66 6.7 3.1 4.4 1.4 versicolor
67 5.6 3.0 4.5 1.5 versicolor
68 5.8 2.7 4.1 1.0 versicolor
69 6.2 2.2 4.5 1.5 versicolor
70 5.6 2.5 3.9 1.1 versicolor
71 5.9 3.2 4.8 1.8 versicolor check
72 6.1 2.8 4.0 1.3 versicolor
73 6.3 2.5 4.9 1.5 versicolor
74 6.1 2.8 4.7 1.2 versicolor check
75 6.4 2.9 4.3 1.3 versicolor
76 6.6 3.0 4.4 1.4 versicolor
77 6.8 2.8 4.8 1.4 versicolor
78 6.7 3.0 5.0 1.7 versicolor check
79 6.0 2.9 4.5 1.5 versicolor
80 5.7 2.6 3.5 1.0 versicolor
81 5.5 2.4 3.8 1.1 versicolor
82 5.5 2.4 3.7 1.0 versicolor
83 5.8 2.7 3.9 1.2 versicolor check
84 6.0 2.7 5.1 1.6 versicolor
85 5.4 3.0 4.5 1.5 versicolor
86 6.0 3.4 4.5 1.6 versicolor
87 6.7 3.1 4.7 1.5 versicolor
88 6.3 2.3 4.4 1.3 versicolor
89 5.6 3.0 4.1 1.3 versicolor
90 5.5 2.5 4.0 1.3 versicolor
91 5.5 2.6 4.4 1.2 versicolor check
92 6.1 3.0 4.6 1.4 versicolor
93 5.8 2.6 4.0 1.2 versicolor check
94 5.0 2.3 3.3 1.0 versicolor
95 5.6 2.7 4.2 1.3 versicolor
96 5.7 3.0 4.2 1.2 versicolor check
97 5.7 2.9 4.2 1.3 versicolor
98 6.2 2.9 4.3 1.3 versicolor
99 5.1 2.5 3.0 1.1 versicolor
100 5.7 2.8 4.1 1.3 versicolor
101 6.3 3.3 6.0 2.5 virginica
102 5.8 2.7 5.1 1.9 virginica check
103 7.1 3.0 5.9 2.1 virginica
104 6.3 2.9 5.6 1.8 virginica check
105 6.5 3.0 5.8 2.2 virginica
106 7.6 3.0 6.6 2.1 virginica
107 4.9 2.5 4.5 1.7 virginica check
108 7.3 2.9 6.3 1.8 virginica check
109 6.7 2.5 5.8 1.8 virginica check
110 7.2 3.6 6.1 2.5 virginica
111 6.5 3.2 5.1 2.0 virginica
112 6.4 2.7 5.3 1.9 virginica check
113 6.8 3.0 5.5 2.1 virginica
114 5.7 2.5 5.0 2.0 virginica
115 5.8 2.8 5.1 2.4 virginica
116 6.4 3.2 5.3 2.3 virginica
117 6.5 3.0 5.5 1.8 virginica check
118 7.7 3.8 6.7 2.2 virginica
119 7.7 2.6 6.9 2.3 virginica
120 6.0 2.2 5.0 1.5 virginica
121 6.9 3.2 5.7 2.3 virginica
122 5.6 2.8 4.9 2.0 virginica
123 7.7 2.8 6.7 2.0 virginica
124 6.3 2.7 4.9 1.8 virginica check
125 6.7 3.3 5.7 2.1 virginica
126 7.2 3.2 6.0 1.8 virginica check
127 6.2 2.8 4.8 1.8 virginica check
128 6.1 3.0 4.9 1.8 virginica check
129 6.4 2.8 5.6 2.1 virginica
130 7.2 3.0 5.8 1.6 virginica
131 7.4 2.8 6.1 1.9 virginica check
132 7.9 3.8 6.4 2.0 virginica
133 6.4 2.8 5.6 2.2 virginica
134 6.3 2.8 5.1 1.5 virginica
135 6.1 2.6 5.6 1.4 virginica
136 7.7 3.0 6.1 2.3 virginica
137 6.3 3.4 5.6 2.4 virginica
138 6.4 3.1 5.5 1.8 virginica check
139 6.0 3.0 4.8 1.8 virginica check
140 6.9 3.1 5.4 2.1 virginica
141 6.7 3.1 5.6 2.4 virginica
142 6.9 3.1 5.1 2.3 virginica
143 5.8 2.7 5.1 1.9 virginica check
144 6.8 3.2 5.9 2.3 virginica
145 6.7 3.3 5.7 2.5 virginica
146 6.7 3.0 5.2 2.3 virginica
147 6.3 2.5 5.0 1.9 virginica check
148 6.5 3.0 5.2 2.0 virginica
149 6.2 3.4 5.4 2.3 virginica
150 5.9 3.0 5.1 1.8 virginica check

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