[英]Tidyverse: how to bind together two column vectors
我正在尝试将以下两个向量绑定在一起:
library(dplyr)
library(mvtnorm)
x.points <- seq(-3, 3, length.out = 100)
y.points <- seq(-3, 3, length.out = 100)
我可以在基础 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
我已尝试按照此答案中的建议使用 dplyr ,但效果不佳:
data2 <- bind_cols(x.points, y.points, c("x","y"))
#> Error: Can't recycle `..1` (size 100) to match `..3` (size 2).
我对上面链接的答案中提到的设置“内部名称”和“外部名称”之间的区别感到困惑:“行需要像c(col1 = 1, col2 = 2)
这样的内部名称,而列需要outer names: col1 = c(1, 2)
。”
由代表 package (v0.3.0) 于 2021 年 4 月 8 日创建
它确实有效,只是不要忘记当您使用reprex
package 来包含库时(此处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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.