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
new_function
and use x as the input. Apply
this function to the numeric columns of the iris_lab
data.
Hint: start with function(x){}
. Fill in the curly
brackets.new_function <- function(x){(x+2)/6}
iris_lab %>%
mutate(across(.cols = !Species, ~ new_function(.x))) %>%
head()
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 1.183333 0.9166667 0.5666667 0.3666667 setosa
## 2 1.150000 0.8333333 0.5666667 0.3666667 setosa
## 3 1.116667 0.8666667 0.5500000 0.3666667 setosa
## 4 1.100000 0.8500000 0.5833333 0.3666667 setosa
## 5 1.166667 0.9333333 0.5666667 0.3666667 setosa
## 6 1.233333 0.9833333 0.6166667 0.4000000 setosa
div_function
and use it to divide x by y and then
add 3 to the outcome. Hint: start with function(x, y){}
.
Fill in the curly brackets.div_function <- function(x, y){(x/y)+ 3}
if_all()
to check if there are iris flowers with
sepal and petal widths and lengths (all the iris_lab
numeric variables) 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