knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
The following code loads a data set about the features of zoo animals.
zoo_df <- read_csv("https://raw.githubusercontent.com/mraynolds/data_607/refs/heads/main/zoo_data-1.csv")
## Rows: 101 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): animal_name
## dbl (16): hair, feathers, eggs, milk, airborne, aquatic, predator, toothed, ...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(zoo_df)
## # A tibble: 6 × 17
## animal_name hair feathers eggs milk airborne aquatic predator toothed
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 aardvark 1 0 0 1 0 0 1 1
## 2 antelope 1 0 0 1 0 0 0 1
## 3 bass 0 0 1 0 0 1 1 1
## 4 bear 1 0 0 1 0 0 1 1
## 5 boar 1 0 0 1 0 0 1 1
## 6 buffalo 1 0 0 1 0 0 0 1
## # ℹ 8 more variables: backbone <dbl>, breathes <dbl>, venomous <dbl>,
## # fins <dbl>, legs <dbl>, tail <dbl>, domestic <dbl>, catsize <dbl>
The following code uses across to convert all the binary indicators of animal features to logical.
zoo <- zoo_df |>
mutate(across(!animal_name & !legs,as.logical))
head(zoo)
## # A tibble: 6 × 17
## animal_name hair feathers eggs milk airborne aquatic predator toothed
## <chr> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl>
## 1 aardvark TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
## 2 antelope TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
## 3 bass FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE
## 4 bear TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
## 5 boar TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
## 6 buffalo TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
## # ℹ 8 more variables: backbone <lgl>, breathes <lgl>, venomous <lgl>,
## # fins <lgl>, legs <dbl>, tail <lgl>, domestic <lgl>, catsize <lgl>