Why can't I add variables with 1x1 tibbles to a tibble with tibble::add_row?

时间:2019-04-17 00:18:51

标签: r tidyverse tibble

This is a simple example of what I am trying to do. Why can't I add a row with each variable a 1x1 tibble?

d <- tibble(x = 2, y = 3)
x1 <- tibble(1)
y1 <- tibble(7)

d2 <- d %>%
  tibble::add_row(
    x = x1,
    y = y1
  )

I know the following works but it is not tidy and it just seems like there should be a better way.

d <- tibble(x = 2, y = 3)
x1 <- tibble(1)
y1 <- tibble(7)

d2 <- d %>%
  tibble::add_row(
    x = as.numeric(x1),
    y = as.numeric(y1)
  )

Thanks in advance awesome R people (:

1 个答案:

答案 0 :(得分:0)

将评论升级为答案:

您要在第一次尝试中将整个数据集(在这种情况下为“ tibble”)插入单个单元格。您需要从数据集中选择单个列以添加为行。

add_row(d, x = x1[[1]], y = y1[[1]])

运行as.numeric(x1)还将x1转换为向量,如您所知,也可以使用。

您还可以完整使用 dplyr 并执行以下操作:

add_row(d, x = pull(x1,1), y = pull(y1,1))

...如果您想避免使用[[括号。