【发布时间】:2022-07-07 19:18:02
【问题描述】:
当组合具有不同行数/长度的数据框和向量时,bind_cols 会出错,而 cbind 会重复行 - 这是为什么呢?
(将其作为 cbind 的默认行为真的明智吗?)
请参阅下面的示例数据。
# Example data
x10 <- c(1:10)
y10 <- c(1:10)
xy10 <- tibble(x10, y10)
z10 <- c(1:10)
z20 <- c(1:20)
# Binding xy and z
xyz10 <- cbind(xy10, z10)
xyz10
# Provide an error
xyz20 <- dplyr::bind_cols(xy10, z20)
# But why is cbind repeating rows of xy10 to suit z20?
xyz20 <- cbind(xy10, z20)
xyz20
【问题讨论】:
-
来自
bind_colsWhen column-binding, rows are matched by position, so all data frames must have the same number of rows. To match by value, not position的文档 -
但是
cbind将重复该向量,而它是参数 1 的向量长度的倍数
标签: r