cor_body <- pg %>%
drop_na() %>%
group_by(species) %>%
summarise(across(.cols = bill_length_mm:flipper_length_mm, ~cor(., body_mass_g)))
cor_body
## # A tibble: 3 x 4
## species bill_length_mm bill_depth_mm flipper_length_mm
## <fct> <dbl> <dbl> <dbl>
## 1 Adelie 0.544 0.580 0.465
## 2 Chinstrap 0.514 0.604 0.642
## 3 Gentoo 0.667 0.723 0.711
cor_max <- cor_body %>%
summarise(across(.cols = bill_length_mm:flipper_length_mm, ~max(.))) %>%
summarise(.[which.max(.)])
cor_max
## # A tibble: 1 x 1
## bill_depth_mm
## <dbl>
## 1 0.723
cor_body %>%
filter_all(any_vars(. %in% c(cor_max$bill_depth_mm)))
## # A tibble: 1 x 4
## species bill_length_mm bill_depth_mm flipper_length_mm
## <fct> <dbl> <dbl> <dbl>
## 1 Gentoo 0.667 0.723 0.711
cor_body %>%
filter(across(.cols = everything(),
.fns = ~(.x %in% c(cor_max$bill_depth_mm))))
## # A tibble: 0 x 4
## # ... with 4 variables: species <fct>, bill_length_mm <dbl>,
## # bill_depth_mm <dbl>, flipper_length_mm <dbl>
※ filter(), We cannot directly use across() in filter() because we need an extra step to combine the results. …
rowAny <- function(x) rowSums(x) > 0
cor_body %>%
filter(
rowAny(
across(.cols = everything(),
.fns = ~(.x %in% c(cor_max$bill_depth_mm))))
)
## # A tibble: 1 x 4
## species bill_length_mm bill_depth_mm flipper_length_mm
## <fct> <dbl> <dbl> <dbl>
## 1 Gentoo 0.667 0.723 0.711
cor_body %>%
filter(if_any(.cols = 2:4, ~(. %in% c(cor_max$bill_depth_mm))))
## # A tibble: 1 x 4
## species bill_length_mm bill_depth_mm flipper_length_mm
## <fct> <dbl> <dbl> <dbl>
## 1 Gentoo 0.667 0.723 0.711