Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`bind_cols()` should recycle a 1-row df #3170

Closed
topepo opened this issue Oct 27, 2017 · 3 comments
Closed

`bind_cols()` should recycle a 1-row df #3170

topepo opened this issue Oct 27, 2017 · 3 comments
Labels

Comments

@topepo
Copy link
Member

@topepo topepo commented Oct 27, 2017

> one <- as_tibble(head(iris))
> two <- tibble(x1 = 1, x2 = 2)
> 
> # For this operation:
> as_tibble(cbind(one, two))
# A tibble: 6 x 7
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species    x1    x2
*        <dbl>       <dbl>        <dbl>       <dbl>  <fctr> <dbl> <dbl>
1          5.1         3.5          1.4         0.2  setosa     1     2
2          4.9         3.0          1.4         0.2  setosa     1     2
3          4.7         3.2          1.3         0.2  setosa     1     2
4          4.6         3.1          1.5         0.2  setosa     1     2
5          5.0         3.6          1.4         0.2  setosa     1     2
6          5.4         3.9          1.7         0.4  setosa     1     2
> 
> # we would expect this to work too:
> bind_cols(one, two)
Error in cbind_all(x) : Argument 2 must be length 1, not 6
> 
> session_info()
─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.3.3 (2017-03-06)
 os       macOS Sierra 10.12.6        
 system   x86_64, darwin13.4.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2017-10-27Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date       source                          
 assertthat    0.2.0      2017-04-11 CRAN (R 3.3.2)                  
 bindr         0.1        2016-11-13 CRAN (R 3.3.2)                  
 bindrcpp      0.2        2017-06-17 CRAN (R 3.3.2)                  
 clisymbols    1.2.0      2017-05-21 CRAN (R 3.3.2)                  
 dplyr       * 0.7.4      2017-09-28 CRAN (R 3.3.2)                  
 glue          1.1.1      2017-06-21 CRAN (R 3.3.2)                  
 magrittr      1.5        2014-11-22 CRAN (R 3.3.0)                  
 pkgconfig     2.0.1      2017-03-21 CRAN (R 3.3.2)                  
 R6            2.2.2      2017-06-17 CRAN (R 3.3.2)                  
 Rcpp          0.12.13    2017-09-28 CRAN (R 3.3.2)                  
 rlang         0.1.2.9000 2017-10-23 Github (tidyverse/rlang@cbdc3f3)
 sessioninfo * 1.0.1      2017-10-02 local (topepo/sessioninfo@NA)   
 tibble        1.3.4      2017-08-22 CRAN (R 3.3.2)                  
 withr         2.0.0      2017-07-28 CRAN (R 3.3.2)                  
 yaml          2.1.14     2016-11-12 CRAN (R 3.3.2)   
@hadley
Copy link
Member

@hadley hadley commented Nov 2, 2017

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session. 😉

(Also, no need for a session info unless it's asked for)

@hadley hadley added the reprex label Nov 2, 2017
@markdly
Copy link
Contributor

@markdly markdly commented Dec 5, 2017

Adding a (slightly more minimal) reprex based on OP:

library(dplyr)
one <- as_tibble(head(iris, 3))
two <- tibble(x1 = 1, x2 = 2)

# For this operation:
as_tibble(cbind(one, two))
#> # A tibble: 3 x 7
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species    x1    x2
#> *        <dbl>       <dbl>        <dbl>       <dbl>  <fctr> <dbl> <dbl>
#> 1          5.1         3.5          1.4         0.2  setosa     1     2
#> 2          4.9         3.0          1.4         0.2  setosa     1     2
#> 3          4.7         3.2          1.3         0.2  setosa     1     2

# we would expect this to work too:
bind_cols(one, two)
#> Error in cbind_all(x): Argument 2 must be length 1, not 3
@krlmlr krlmlr added feature data frame and removed reprex labels Jan 19, 2018
@krlmlr
Copy link
Member

@krlmlr krlmlr commented Jan 19, 2018

Thanks. I'm not sure we want this, because this would add a lot of complexity to the implementation, and allow for mistakes. You can already use add_column() and unquote-splice:

library(tibble)
library(magrittr)
to_add <- tibble(a = 1, b = 2)
mtcars[1:2] %>% add_column(!!! to_add) %>% head()
#>                    mpg cyl a b
#> Mazda RX4         21.0   6 1 2
#> Mazda RX4 Wag     21.0   6 1 2
#> Datsun 710        22.8   4 1 2
#> Hornet 4 Drive    21.4   6 1 2
#> Hornet Sportabout 18.7   8 1 2
#> Valiant           18.1   6 1 2

Created on 2018-01-19 by the reprex package (v0.1.1.9000).

@hadley hadley closed this Jan 19, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants