Skip to content

Commit 7947507

Browse files
authored
Replace vec_repeat() with vec_rep() and vec_rep_each() (#1131)
* speed up `vec_repeat()` * replace `vec_repeat()` * Refactor `expand_grid()`
1 parent 36e5399 commit 7947507

File tree

4 files changed

+11
-18
lines changed

4 files changed

+11
-18
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# tidyr (development version)
22

3+
* `expand_grid()` is now about twice as fast and `pivot_wider()` is a bit faster
4+
(@mgirlich, #1130).
5+
36
* `unnest()` is now much faster (@mgirlich, @DavisVaughan, #1127).
47

58
* `unnest()` no longer allows unnesting a list-col containing a mix of vector

R/expand.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ expand_grid <- function(..., .name_repair = "check_unique") {
167167
if (n == 0) {
168168
out <- map(dots, vec_slice, integer())
169169
} else {
170-
each <- n / cumprod(ns)
171-
times <- n / each / ns
172-
173-
out <- pmap(list(x = dots, each = each, times = times), vec_repeat)
170+
times <- n / cumprod(ns)
171+
out <- map2(dots, times, vec_rep_each)
172+
times <- n / times / ns
173+
out <- map2(out, times, vec_rep)
174174
}
175175
out <- as_tibble(out, .name_repair = .name_repair)
176176

R/pivot-wide.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pivot_wider_spec <- function(data,
261261
} else {
262262
stopifnot(vec_size(fill) == 1)
263263
fill <- vec_cast(fill, val)
264-
out <- vec_repeat(fill, nrow * ncol)
264+
out <- vec_rep_each(fill, nrow * ncol)
265265
}
266266
vec_slice(out, val_id$row + nrow * (val_id$col - 1L)) <- val
267267

@@ -308,11 +308,11 @@ build_wider_spec <- function(data,
308308
if (length(values_from) == 1) {
309309
out$.value <- names(values_from)
310310
} else {
311-
out <- vec_repeat(out, times = vec_size(values_from))
312-
out$.value <- vec_repeat(names(values_from), each = vec_size(row_ids))
311+
out <- vec_rep(out, vec_size(values_from))
312+
out$.value <- vec_rep_each(names(values_from), vec_size(row_ids))
313313
out$.name <- paste0(out$.value, names_sep, out$.name)
314314

315-
row_ids <- vec_repeat(row_ids, times = vec_size(values_from))
315+
row_ids <- vec_rep(row_ids, vec_size(values_from))
316316
}
317317

318318
out <- vec_cbind(out, as_tibble(row_ids), .name_repair = "minimal")

R/utils.R

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,6 @@ tidyr_col_modify <- function(data, cols) {
114114
data
115115
}
116116

117-
# Own copy since it might disappear from vctrs since it
118-
# isn't well thought out
119-
vec_repeat <- function(x, each = 1L, times = 1L) {
120-
vec_assert(each, size = 1L)
121-
vec_assert(times, size = 1L)
122-
123-
idx <- rep(vec_seq_along(x), times = times, each = each)
124-
vec_slice(x, idx)
125-
}
126-
127117
check_present <- function(x) {
128118
arg <- ensym(x)
129119
if (missing(x)) {

0 commit comments

Comments
 (0)