Closed huftis closed 2 years ago
Here's my stab at it:
interleave <- function(..., .ptype = NULL) {
args <- list2(...)
p <- length(args)
if (p == 0)
return(NULL)
args <- vec_recycle(!!!args)
vals <- vec_c(!!!args, .ptype = .ptype)
idx <- matrix(seq_along(vals), nrow = p, byrow = TRUE)
vals[as.integer(idx)]
}
Small update:
(I suppose in theory name repair and the name spec should be passed on to vec_c
)
library(vctrs)
library(rlang)
vec_interleave <- function(..., .ptype = NULL) {
args <- list2(...)
n_args <- vec_size(args)
if (n_args == 0L) {
return(NULL)
}
args <- vec_recycle_common(!!! args)
out <- vec_c(!!! args, .ptype = .ptype)
pos <- vec_seq_along(out)
pos <- as.integer(matrix(pos, nrow = n_args, byrow = TRUE))
vec_slice(out, pos)
}
vec_interleave(1:5, 6:10, 11:15)
#> [1] 1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
cars <- mtcars[1:3,]
vec_interleave(cars, vec_init(cars))
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> 2 NA NA NA NA NA NA NA NA NA NA NA
#> 3 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> 4 NA NA NA NA NA NA NA NA NA NA NA
#> 5 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> 6 NA NA NA NA NA NA NA NA NA NA NA
library(vctrs)
library(rlang)
vec_interleave <- function(..., .ptype = NULL, .name_spec = NULL, .name_repair = c("minimal", "unique", "check_unique", "universal")) {
args <- list2(...)
n_args <- vec_size(args)
if (n_args == 0L) {
return(NULL)
}
args <- vec_recycle_common(!!! args)
out <- vec_c(!!! args, .ptype = .ptype, .name_spec = .name_spec, .name_repair = .name_repair)
pos <- vec_seq_along(out)
pos <- as.integer(matrix(pos, nrow = n_args, byrow = TRUE))
vec_slice(out, pos)
}
vec_interleave(1:5, 6:10, 11:15)
#> [1] 1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
vec_interleave(a = 1:5, b = 6:10, .name_spec = "{outer}")
#> a b a b a b a b a b
#> 1 6 2 7 3 8 4 9 5 10
cars <- mtcars[1:3,]
not sure about this though
vec_interleave(cars, vec_init(cars))
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> ...2 NA NA NA NA NA NA NA NA NA NA NA
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> ...4 NA NA NA NA NA NA NA NA NA NA NA
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> ...6 NA NA NA NA NA NA NA NA NA NA NA
which happens because ...n
seems to have special treatment
df <- data.frame(x = 1:3)
row.names(df) <- c("a", "...2", "b")
df
#> x
#> a 1
#> ...2 2
#> b 3
vec_slice(df, c(1, 3, 2))
#> x
#> a 1
#> b 3
#> ...3 2
Created on 2021-05-05 by the reprex package (v2.0.0)
Yea initializing a data frame with vec_init(x)
always gives unique row names to start with (if x
originally had row names), and then vec_c()
and vec_slice()
with data frames will silently strip any ...n
and then re-add them to correctly reflect the new positions. So I think it is ok?
library(vctrs)
#> Warning: package 'vctrs' was built under R version 4.0.2
x <- mtcars[1:2,]
y <- vec_init(mtcars, 2)
x
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
y
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> ...1 NA NA NA NA NA NA NA NA NA NA NA
#> ...2 NA NA NA NA NA NA NA NA NA NA NA
xy <- vec_c(x, y)
xy
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
#> ...3 NA NA NA NA NA NA NA NA NA NA NA
#> ...4 NA NA NA NA NA NA NA NA NA NA NA
vec_slice(xy, c(4, 2, 1, 3))
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> ...1 NA NA NA NA NA NA NA NA NA NA NA
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> ...4 NA NA NA NA NA NA NA NA NA NA NA
Created on 2021-05-05 by the reprex package (v1.0.0)
Just discovered this package. A package for handling vectors sounds very useful. Here’s a feature request (with code) for a vector operation I occasionally find use for.
A function for interleaving vectors would be nice. Here’s some code for such a function:
A few examples: