Comments (4)
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)]
}
from funs.
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
from funs.
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)
from funs.
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)
from funs.
Related Issues (20)
- Implement is_distinct()? HOT 2
- A na.rm option for cumany? HOT 1
- na.rm argument in lead and lag? HOT 1
- Revisit `dplyr::coalesce` with `across` HOT 7
- Common suffixes and prefixes of vectors HOT 2
- Make sure if_else() cross-references to case_when()
- Add `mode` to vector functions
- Consider `case_when()` variant that uses values HOT 4
- `coalesce()` invariants HOT 1
- `set_intersect()`, `set_union()`, `set_diff()`, and `set_equal()`
- coalesce on a single column (typically with summarize) HOT 3
- Make a website
- Move `master` branch to `main` HOT 1
- Consider `case_when()` variant returning ordered factors HOT 3
- case_when() should preserve names HOT 1
- `between()` should recycle `left` and `right` to the size of `x`
- Should near have a digits argument?
- Accessible helper for run length encoded style groups HOT 1
- vec_equal_na is deprecated
- `min()` and `max()` variants with an `empty` argument
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from funs.