File: colwise.R

package info (click to toggle)
r-cran-dplyr 1.1.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,292 kB
  • sloc: cpp: 1,403; sh: 17; makefile: 7
file content (154 lines) | stat: -rw-r--r-- 5,480 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = T, comment = "#>")
options(tibble.print_min = 4L, tibble.print_max = 4L)
set.seed(1014)

## ----eval = FALSE-------------------------------------------------------------
#  df %>%
#    group_by(g1, g2) %>%
#    summarise(a = mean(a), b = mean(b), c = mean(c), d = mean(d))

## ----eval = FALSE-------------------------------------------------------------
#  df %>%
#    group_by(g1, g2) %>%
#    summarise(across(a:d, mean))

## ----setup--------------------------------------------------------------------
library(dplyr, warn.conflicts = FALSE)

## -----------------------------------------------------------------------------
starwars %>% 
  summarise(across(where(is.character), n_distinct))

starwars %>% 
  group_by(species) %>% 
  filter(n() > 1) %>% 
  summarise(across(c(sex, gender, homeworld), n_distinct))

starwars %>% 
  group_by(homeworld) %>% 
  filter(n() > 1) %>% 
  summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE)))

## -----------------------------------------------------------------------------
df <- data.frame(g = c(1, 1, 2), x = c(-1, 1, 3), y = c(-1, -4, -9))
df %>% 
  group_by(g) %>% 
  summarise(across(where(is.numeric), sum))

## -----------------------------------------------------------------------------
min_max <- list(
  min = ~min(.x, na.rm = TRUE), 
  max = ~max(.x, na.rm = TRUE)
)
starwars %>% summarise(across(where(is.numeric), min_max))
starwars %>% summarise(across(c(height, mass, birth_year), min_max))

## -----------------------------------------------------------------------------
starwars %>% summarise(across(where(is.numeric), min_max, .names = "{.fn}.{.col}"))
starwars %>% summarise(across(c(height, mass, birth_year), min_max, .names = "{.fn}.{.col}"))

## -----------------------------------------------------------------------------
starwars %>% summarise(
  across(c(height, mass, birth_year), ~min(.x, na.rm = TRUE), .names = "min_{.col}"),
  across(c(height, mass, birth_year), ~max(.x, na.rm = TRUE), .names = "max_{.col}")
)

## -----------------------------------------------------------------------------
starwars %>% summarise(
  tibble(
    across(where(is.numeric), ~min(.x, na.rm = TRUE), .names = "min_{.col}"),
    across(where(is.numeric), ~max(.x, na.rm = TRUE), .names = "max_{.col}")  
  )
)

## -----------------------------------------------------------------------------
starwars %>% 
  summarise(across(where(is.numeric), min_max, .names = "{.fn}.{.col}")) %>% 
  relocate(starts_with("min"))

## -----------------------------------------------------------------------------
df <- tibble(x = 1:3, y = 3:5, z = 5:7)
mult <- list(x = 1, y = 10, z = 100)

df %>% mutate(across(all_of(names(mult)), ~ .x * mult[[cur_column()]]))

## -----------------------------------------------------------------------------
df <- data.frame(x = c(1, 2, 3), y = c(1, 4, 9))

df %>% 
  summarise(n = n(), across(where(is.numeric), sd))

## -----------------------------------------------------------------------------
df %>% 
  summarise(across(where(is.numeric), sd), n = n())

## -----------------------------------------------------------------------------
df %>% 
  summarise(n = n(), across(where(is.numeric) & !n, sd))

## -----------------------------------------------------------------------------
df %>% 
  summarise(
    tibble(n = n(), across(where(is.numeric), sd))
  )

## -----------------------------------------------------------------------------
rescale01 <- function(x) {
  rng <- range(x, na.rm = TRUE)
  (x - rng[1]) / (rng[2] - rng[1])
}
df <- tibble(x = 1:4, y = rnorm(4))
df %>% mutate(across(where(is.numeric), rescale01))

## -----------------------------------------------------------------------------
starwars %>% distinct(pick(contains("color")))

## -----------------------------------------------------------------------------
starwars %>% count(pick(contains("color")), sort = TRUE)

## -----------------------------------------------------------------------------
starwars %>% 
  filter(if_any(everything(), ~ !is.na(.x)))

## -----------------------------------------------------------------------------
starwars %>% 
  filter(if_all(everything(), ~ !is.na(.x)))

## ----eval = FALSE-------------------------------------------------------------
#  df %>%
#    group_by(g1, g2) %>%
#    summarise(
#      across(where(is.numeric), mean),
#      across(where(is.factor), nlevels),
#      n = n(),
#    )

## ----results = FALSE----------------------------------------------------------
df %>% mutate_if(is.numeric, ~mean(.x, na.rm = TRUE))
# ->
df %>% mutate(across(where(is.numeric), ~mean(.x, na.rm = TRUE)))

df %>% mutate_at(vars(c(x, starts_with("y"))), mean)
# ->
df %>% mutate(across(c(x, starts_with("y")), mean))

df %>% mutate_all(mean)
# ->
df %>% mutate(across(everything(), mean))

## -----------------------------------------------------------------------------
df <- tibble(x = c("a", "b"), y = c(1, 1), z = c(-1, 1))

# Find all rows where EVERY numeric variable is greater than zero
df %>% filter(if_all(where(is.numeric), ~ .x > 0))

# Find all rows where ANY numeric variable is greater than zero
df %>% filter(if_any(where(is.numeric), ~ .x > 0))

## -----------------------------------------------------------------------------
df <- tibble(x = 2, y = 4, z = 8)
df %>% mutate_all(~ .x / y)

df %>% mutate(across(everything(), ~ .x / y))