Skip to content

Commit d6bfc3b

Browse files
committed
Implement tidyr_chop2()
Since we don't have `vec_chop2()` yet r-lib/vctrs#1226
1 parent e7aac41 commit d6bfc3b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

R/utils.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ tidyr_new_list <- function(x) {
130130
x
131131
}
132132

133+
# What `vec_chop2()` would be.
134+
# Equivalent to `vec_chop(x)`, but moves names of `x` to the result.
135+
tidyr_chop2 <- function(x) {
136+
names <- vec_names(x)
137+
138+
if (!is.null(names)) {
139+
x <- vec_set_names(x, NULL)
140+
}
141+
142+
out <- vec_chop(x)
143+
144+
if (!is.null(names)) {
145+
out <- vec_set_names(out, names)
146+
}
147+
148+
out
149+
}
150+
133151
apply_names_sep <- function(outer, inner, names_sep) {
134152
as.character(glue("{outer}{names_sep}{inner}"))
135153
}

0 commit comments

Comments
 (0)