0

I am trying to bind together the two following vectors:

library(dplyr)
library(mvtnorm)

x.points <- seq(-3, 3, length.out = 100)
y.points <- seq(-3, 3, length.out = 100)

I can do it easily in base R:

data1 <- cbind(x.points, y.points) 
head(data1)
#>       x.points  y.points
#> [1,] -3.000000 -3.000000
#> [2,] -2.939394 -2.939394
#> [3,] -2.878788 -2.878788
#> [4,] -2.818182 -2.818182
#> [5,] -2.757576 -2.757576
#> [6,] -2.696970 -2.696970

I have tried using dplyr following the suggestion in this answer but it did not work that well:

data2 <- bind_cols(x.points, y.points, c("x","y"))

#> Error: Can't recycle `..1` (size 100) to match `..3` (size 2).

I am confused about the difference between setting up "inner names" and "outer names" mentioned in the answer linked above: "Rows require inner names like c(col1 = 1, col2 = 2), while columns require outer names: col1 = c(1, 2)."

Created on 2021-04-08 by the reprex package (v0.3.0)

Waldi
  • 39,242
  • 6
  • 30
  • 78
Emy
  • 817
  • 1
  • 8
  • 25
  • 2
    `bind_cols(x.points, y.points)`. It looks like you didn't run `library(dplyr)` first. – Matt Apr 08 '21 at 19:47
  • 2
    did you load the dplyr package with `library(dplyr)`? does `dplyr::bind_cols` work? – emilliman5 Apr 08 '21 at 19:47
  • @Matt I have edited the question, there was a mistake in the reprex (like everybody noticed, I had forgotten to include the libraries in selection.) Once that error is fixed, there is a different error popping up, related to the way I was trying to rename the columns. – Emy Apr 09 '21 at 12:26
  • Not sure if you still need clarification, but as I did in my answer, the outer name `x` for the column is outside the vector defining the column `x.points`. – Waldi Apr 13 '21 at 10:18

1 Answers1

1

It actually works, just don't forget when you use the reprex package to include the libraries (here dplyr)

library(dplyr)
x.points <- seq(-3, 3, length.out = 100)
y.points <- seq(-3, 3, length.out = 100)
data2 <- bind_cols(x=x.points, y=y.points)
data2
#> # A tibble: 100 x 2
#>        x     y
#>    <dbl> <dbl>
#>  1 -3    -3   
#>  2 -2.94 -2.94
#>  3 -2.88 -2.88
#>  4 -2.82 -2.82
#>  5 -2.76 -2.76
#>  6 -2.70 -2.70
#>  7 -2.64 -2.64
#>  8 -2.58 -2.58
#>  9 -2.52 -2.52
#> 10 -2.45 -2.45
#> # ... with 90 more rows
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • I have edited the question, there was a mistake in the reprex (like everybody noticed, I had forgotten to include the libraries in selection.) Once that error is fixed, there is a different error popping up, related to the way I was trying to rename the columns. I was confused about the difference between setting up 'inner names' and `outer names` in `bind_rows` and `bind_cols` respectively. – Emy Apr 09 '21 at 12:26