为什么以下代码不起作用。
我想在我之前从 tibble 复制的 tibble 中添加一行。
library(dplyr)
library(tibble)
tiris <- as_tibble(iris)
new_row <- tiris %>% tail(1)
tiris <- tiris %>% add_row(new_row)
错误:列
new_row
必须是一维原子向量或列表
最佳答案
new_row
您创建的是一行 tibble
,所以我认为您需要的是 bind_rows
来自 dplyr 的函数包,可以组合两个 tibble
或 data frame
按行。
library(dplyr)
library(tibble)
tiris <- as_tibble(iris)
new_row <- tiris %>% tail(1)
# Combine tiris and new_row
tiris <- tiris %>% bind_rows(new_row)
查看
tiris
的最后两行,它们是相同的。所以我想bind_rows
作品。# View the results
tail(tiris, 2)
# # A tibble: 2 x 5
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# <dbl> <dbl> <dbl> <dbl> <fct>
# 1 5.90 3.00 5.10 1.80 virginica
# 2 5.90 3.00 5.10 1.80 virginica
关于r - tibble add_row 不整洁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48276883/