Keep observations for a specific subgroup

Viewed 19

I’m brand new to R so I’m sorry if this is a dumb question. I am working on an exercise that states “for tibble student_data, exclude caucasians.” We are using tidyverse, so I know that it starts… student_data %>% I get lost from there. I can’t find anything about it online. Any help would be greatly appreciated

As I mentioned, I know it starts student_data %>% I’ve tried filter, mutate, etc. Maybe I’m not using it correctly

1 Answers
library(tidyverse)

# Make fake data
fake_df <- tibble::tibble(
  ethnicity = rep(c("Caucasian", "Non-Caucasian"),5),
  fake_column1 = 1:10
)

fake_df
#> # A tibble: 10 × 2
#>    ethnicity     fake_column1
#>    <chr>                <int>
#>  1 Caucasian                1
#>  2 Non-Caucasian            2
#>  3 Caucasian                3
#>  4 Non-Caucasian            4
#>  5 Caucasian                5
#>  6 Non-Caucasian            6
#>  7 Caucasian                7
#>  8 Non-Caucasian            8
#>  9 Caucasian                9
#> 10 Non-Caucasian           10

filtered_fake <- fake_df %>%
  # Filter out rows where ethnicity is "Caucasian"
  filter(ethnicity != "Caucasian")

# Check results
filtered_fake
#> # A tibble: 5 × 2
#>   ethnicity     fake_column1
#>   <chr>                <int>
#> 1 Non-Caucasian            2
#> 2 Non-Caucasian            4
#> 3 Non-Caucasian            6
#> 4 Non-Caucasian            8
#> 5 Non-Caucasian           10

Created on 2022-09-09 by the reprex package (v2.0.1)

Related