Tidyverse:如何将两个列向量绑定在一起 [英] Tidyverse: how to bind together two column vectors

查看:95
本文介绍了Tidyverse:如何将两个列向量绑定在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下两个向量绑定在一起:

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)

我可以在R底下轻松地做到这一点:

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

在此答案中,我尝试使用dplyr遵循建议 ,但效果不佳:

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).

我对设置内部名称"和内部名称"之间的区别感到困惑.和外部名称"在上面的答案链接中提到:行需要内部名称,例如 c(col1 = 1,col2 = 2),而列则需要外部名称:col1 = c(1,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)."

reprex软件包(v0.3.0)创建于2021-04-08 sup>

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

推荐答案

它确实有效,只是不要忘记何时使用 reprex 包包括库(此处为 dplyr )

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

这篇关于Tidyverse:如何将两个列向量绑定在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆