Tidyverse学习笔记

Tidyverse

发布于

2024年12月3日

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.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

1 c_across()

dplyr文档中介绍到,与c()相比,(1)c_across()使用tidy select,更加便捷; (2)c_across()使用vctrs::vec_c(),给出的输出更加安全。

tidy select只有15种(见help("select"))::!&c()everything()last_col()group_cols()starts_with()ends_with()contains()matches()num_range()all_of()any_of()where()

下面的例子对c()c_across()进行了比较。 尽管其结果看起来一样,dplyr文档中推荐的用法是将rowwise()c_across(X1:X10)配合使用。

toy_dat <- data.frame(matrix(1:100, nrow = 10, byrow = TRUE))
toy_dat
   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1   1  2  3  4  5  6  7  8  9  10
2  11 12 13 14 15 16 17 18 19  20
3  21 22 23 24 25 26 27 28 29  30
4  31 32 33 34 35 36 37 38 39  40
5  41 42 43 44 45 46 47 48 49  50
6  51 52 53 54 55 56 57 58 59  60
7  61 62 63 64 65 66 67 68 69  70
8  71 72 73 74 75 76 77 78 79  80
9  81 82 83 84 85 86 87 88 89  90
10 91 92 93 94 95 96 97 98 99 100
toy_dat |>
  rowwise() |> 
  mutate(
    z1 = mean(X1:X10, na.rm = TRUE),
    z2 = mean(c(X1:X10), na.rm = TRUE),
    z4 = mean(c(X1:X5, X10), na.rm = TRUE),
    z3 = mean(c_across(X1:X10), na.rm = TRUE),
    # z5 = mean(c_across(X1:X5, X10), na.rm = TRUE), # does not work
    z5 = mean(c_across(c(X1:X5, X10)), na.rm = TRUE),
  ) |> 
  ungroup() |> 
  select(z1:z5)
# A tibble: 10 × 5
      z1    z2    z4    z3    z5
   <dbl> <dbl> <dbl> <dbl> <dbl>
 1   5.5   5.5  4.17   5.5  4.17
 2  15.5  15.5 14.2   15.5 14.2 
 3  25.5  25.5 24.2   25.5 24.2 
 4  35.5  35.5 34.2   35.5 34.2 
 5  45.5  45.5 44.2   45.5 44.2 
 6  55.5  55.5 54.2   55.5 54.2 
 7  65.5  65.5 64.2   65.5 64.2 
 8  75.5  75.5 74.2   75.5 74.2 
 9  85.5  85.5 84.2   85.5 84.2 
10  95.5  95.5 94.2   95.5 94.2