Git Product home page Git Product logo

Comments (3)

markfairbanks avatar markfairbanks commented on August 20, 2024 1

@leungi These are great! Would you want to make a pull request with the edits? For the other issue you can open a 2nd PR or combine them both into one, whichever one is easier for you.

If you haven't done a PR before I can walk you through it, or if you prefer I could make the changes myself. Either way afterwards we can add you as a contributor to the package. Let me know how you want to proceed

from tidytable.

leungi avatar leungi commented on August 20, 2024 1

I'll work on the PR; thanks for endorsing!

from tidytable.

leungi avatar leungi commented on August 20, 2024

Similarly for dt_bind_cols().

Another suggestion is taking a page out off dplyr::bind_cols() with their column repair. The current dt_bind_cols() doesn't check/prevent duplicated column names, and creates issue when it happens.

library(tidydt)
#> 
#> Attaching package: 'tidydt'
#> The following object is masked from 'package:stats':
#> 
#>     dt

one <- mtcars[1:4,]
two <- mtcars[11:14,]

# both OK
dplyr::bind_cols(one, two)
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb mpg1 cyl1 disp1 hp1
#> 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 17.8    6 167.6 123
#> 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 16.4    8 275.8 180
#> 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 17.3    8 275.8 180
#> 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 15.2    8 275.8 180
#>   drat1  wt1 qsec1 vs1 am1 gear1 carb1
#> 1  3.92 3.44  18.9   1   0     4     4
#> 2  3.07 4.07  17.4   0   0     3     3
#> 3  3.07 3.73  17.6   0   0     3     3
#> 4  3.07 3.78  18.0   0   0     3     3
dplyr::bind_cols(list(one, two))
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb mpg1 cyl1 disp1 hp1
#> 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 17.8    6 167.6 123
#> 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 16.4    8 275.8 180
#> 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 17.3    8 275.8 180
#> 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 15.2    8 275.8 180
#>   drat1  wt1 qsec1 vs1 am1 gear1 carb1
#> 1  3.92 3.44  18.9   1   0     4     4
#> 2  3.07 4.07  17.4   0   0     3     3
#> 3  3.07 3.73  17.6   0   0     3     3
#> 4  3.07 3.78  18.0   0   0     3     3

# OK
dt_bind_cols(one, two)
#>     mpg cyl disp  hp drat    wt  qsec vs am gear carb  mpg cyl  disp  hp
#> 1: 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 17.8   6 167.6 123
#> 2: 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 16.4   8 275.8 180
#> 3: 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 17.3   8 275.8 180
#> 4: 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 15.2   8 275.8 180
#>    drat   wt qsec vs am gear carb
#> 1: 3.92 3.44 18.9  1  0    4    4
#> 2: 3.07 4.07 17.4  0  0    3    3
#> 3: 3.07 3.73 17.6  0  0    3    3
#> 4: 3.07 3.78 18.0  0  0    3    3

# fails
dt_bind_cols(list(one, two))
#> Error in dt_bind_cols(list(one, two)): All inputs must be a data.frame or data.table

dt_bind_cols_edit <- function (.data, ...)
{
  # check if input .data is already a list; if not, transform to list
  if (class(.data) != "list") {
    .data <- list(.data)
  }
  
  dots <- enexprs(...)
  dots <- dt_map(dots, eval)
  dots <- append(.data, dots)
  if (!all(dt_map_lgl(dots, is.data.frame)))
    stop("All inputs must be a data.frame or data.table")
  if (!all(dt_map_lgl(dots, is.data.table)))
    dots <- dt_map(dots, as.data.table)
  do.call(cbind, dots)

  # name repair from dplyr::bind_cols()
  # tibble::repair_names(do.call(cbind, dots))
}

# both OK
dt_bind_cols_edit(one, two)
#>     mpg cyl disp  hp drat    wt  qsec vs am gear carb  mpg cyl  disp  hp
#> 1: 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 17.8   6 167.6 123
#> 2: 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 16.4   8 275.8 180
#> 3: 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 17.3   8 275.8 180
#> 4: 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 15.2   8 275.8 180
#>    drat   wt qsec vs am gear carb
#> 1: 3.92 3.44 18.9  1  0    4    4
#> 2: 3.07 4.07 17.4  0  0    3    3
#> 3: 3.07 3.73 17.6  0  0    3    3
#> 4: 3.07 3.78 18.0  0  0    3    3
dt_bind_cols_edit(list(one, two))
#>     mpg cyl disp  hp drat    wt  qsec vs am gear carb  mpg cyl  disp  hp
#> 1: 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 17.8   6 167.6 123
#> 2: 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 16.4   8 275.8 180
#> 3: 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 17.3   8 275.8 180
#> 4: 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 15.2   8 275.8 180
#>    drat   wt qsec vs am gear carb
#> 1: 3.92 3.44 18.9  1  0    4    4
#> 2: 3.07 4.07 17.4  0  0    3    3
#> 3: 3.07 3.73 17.6  0  0    3    3
#> 4: 3.07 3.78 18.0  0  0    3    3

Created on 2020-01-19 by the reprex package (v0.3.0)

from tidytable.

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.