tibble add_row 不整洁?
tibble add_row not tidy?
为什么下面的代码不起作用。
我想在我之前从小标题复制的小标题中添加一行。
library(dplyr)
library(tibble)
tiris <- as_tibble(iris)
new_row <- tiris %>% tail(1)
tiris <- tiris %>% add_row(new_row)
错误:列 new_row
必须是一维原子向量或列表
你创建的new_row
是一行tibble
,所以我认为你需要的是dplyr包中的bind_rows
函数,它可以将两个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
为什么下面的代码不起作用。 我想在我之前从小标题复制的小标题中添加一行。
library(dplyr)
library(tibble)
tiris <- as_tibble(iris)
new_row <- tiris %>% tail(1)
tiris <- tiris %>% add_row(new_row)
错误:列 new_row
必须是一维原子向量或列表
你创建的new_row
是一行tibble
,所以我认为你需要的是dplyr包中的bind_rows
函数,它可以将两个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