Giter Club home page Giter Club logo

Comments (4)

hadley avatar hadley commented on June 14, 2024

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.

DavisVaughan avatar DavisVaughan commented on June 14, 2024

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.

romainfrancois avatar romainfrancois commented on June 14, 2024
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.

DavisVaughan avatar DavisVaughan commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.