Check out this purrr tutorial by Jenny Bryan: https://jennybc.github.io/purrr-tutorial/ls02_map-extraction-advanced.html
Additional content to read up on: https://jennybc.github.io/purrr-tutorial/ls01_map-name-position-shortcuts.html
iris
called
iris_lab
. Use this for the lab.iris_lab <- iris
iris_lab
data, use mutate
and across
to change the numeric variables to be rounded
with the round
function. Take a look with
head()
- do this for the rest of the questions in which you
create an output of data.iris_lab %>%
mutate(across(.cols = !Species, round)) %>%
head()
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
## 4 5 3 2 0 setosa
## 5 5 4 1 0 setosa
## 6 5 4 2 0 setosa
# OR
iris_lab %>%
mutate(across(.cols = where(is.numeric), round)) %>%
head()
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
## 4 5 3 2 0 setosa
## 5 5 4 1 0 setosa
## 6 5 4 2 0 setosa
iris_lab %>%
mutate(across(.cols = !Species, ~ round(.x, digits = 1))) %>%
head()
## 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
if_all()
to check if there are iris flowers with
all measurements greater than 2 centimeters but less than 6 centimeters
(which is the unit, if you type in ?iris you can learn more about the
data). Hint - use filter
. If any are, how many?iris_lab %>%
filter(if_all(starts_with(c("Petal", "Sepal")), ~.x > 2 & .x < 6)) %>%
head()
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.8 2.8 5.1 2.4 virginica
format()
with the argument
scientific = TRUE
can be used to format numbers in
scientific notation. For example:format(10000, scientific = TRUE)
## [1] "1e+04"
Use modify_if
to convert numeric columns of
iris_lab
to be in scientific notation.
iris_lab <- iris_lab %>%
modify_if(is.numeric, ~ format(.x, scientific = TRUE))
head(iris_lab)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1e+00 3.5e+00 1.4e+00 2.0e-01 setosa
## 2 4.9e+00 3.0e+00 1.4e+00 2.0e-01 setosa
## 3 4.7e+00 3.2e+00 1.3e+00 2.0e-01 setosa
## 4 4.6e+00 3.1e+00 1.5e+00 2.0e-01 setosa
## 5 5.0e+00 3.6e+00 1.4e+00 2.0e-01 setosa
## 6 5.4e+00 3.9e+00 1.7e+00 4.0e-01 setosa