filter-na3

2023
eda
na
string
Published

May 14, 2023

Aufgabe

Filtern Sie alle Zeilen mit fehlende Werte im Datensatz penguins!

Liefern Sie die Spalten zurück, die fehlende Werte aufweisen.











Lösung

Setup

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── 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
d_path <- "https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv"
d <- read_csv(d_path)
Rows: 344 Columns: 9
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): species, island, sex
dbl (6): rownames, bill_length_mm, bill_depth_mm, flipper_length_mm, body_ma...

ℹ 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.
nrow(d)
[1] 344

Weg 1

d_na_only <-
  d %>% 
  filter(!complete.cases(.)) 

d_na_only %>% 
  names()
[1] "rownames"          "species"           "island"           
[4] "bill_length_mm"    "bill_depth_mm"     "flipper_length_mm"
[7] "body_mass_g"       "sex"               "year"             

Weg 2

d %>% 
  filter(if_any(everything(), ~ is.na(.))) %>% 
  names()
[1] "rownames"          "species"           "island"           
[4] "bill_length_mm"    "bill_depth_mm"     "flipper_length_mm"
[7] "body_mass_g"       "sex"               "year"             

Categories:

  • 2023
  • eda
  • na
  • string