2.1 Introduction
Almost all of the functions that you’ll use in the tidyverse produce tibbles. To coerce a data.frame
into tibble, we can use as_tibble()
:
class(iris)
#> [1] "data.frame"
## 转换为 tibble
iris_tibble <- as_tibble(iris)
iris_tibble
#> # A tibble: 150 x 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
#> # ... with 144 more rows
相比于 data.frame()
,tibble()
创建数据矿的附加操作要少的多:它不能改变输入的类型(例如,不会默认将字符串转变为因子)、变量的名称,也不能创建行名称。
有些比较旧的函数不支持 tibble
,如果遇到这种函数,可以使用 as_data_frame()
转换到传统的 data.frame
上。
add_row(.data, ..., .before = NULL, .after = NULL)
s a convenient way to add one or more rows of data to an existing data frame. ...
should be name-value pairs corresponding to column names and its value. By default new row are added after the last row.
# add_row ---------------------------------
df <- tibble(x = 1:3, y = 3:1)
add_row(df, x = 4, y = 0)
#> # A tibble: 4 x 2
#> x y
#> <dbl> <dbl>
#> 1 1 3
#> 2 2 2
#> 3 3 1
#> 4 4 0
# You can specify where to add the new rows
add_row(df, x = 4, y = 0, .before = 2)
#> # A tibble: 4 x 2
#> x y
#> <dbl> <dbl>
#> 1 1 3
#> 2 4 0
#> 3 2 2
#> 4 3 1
# You can supply vectors, to add multiple rows (this isn't
# recommended because it's a bit hard to read)
add_row(df, x = 4:5, y = 0:-1)
#> # A tibble: 5 x 2
#> x y
#> <int> <int>
#> 1 1 3
#> 2 2 2
#> 3 3 1
#> 4 4 0
#> 5 5 -1
# Absent variables get missing values
add_row(df, x = 4)
#> # A tibble: 4 x 2
#> x y
#> <dbl> <int>
#> 1 1 3
#> 2 2 2
#> 3 3 1
#> 4 4 NA
Yet we cannot create new variables with add_row
:
enframe()
converts named atomic vectors or lists to one- or two-column data frames . For vectors with more than 2 elements, a list column is created.
enframe(c(a = 1, b = 2))
#> # A tibble: 2 x 2
#> name value
#> <chr> <dbl>
#> 1 a 1
#> 2 b 2
# For unnamed vectors, the natural sequence is used as name column.
enframe(1:2)
#> # A tibble: 2 x 2
#> name value
#> <int> <int>
#> 1 1 1
#> 2 2 2
# vectors with more than 2 elements
enframe(1:3)
#> # A tibble: 3 x 2
#> name value
#> <int> <int>
#> 1 1 1
#> 2 2 2
#> 3 3 3
For a list, the result will also be a list column.
enframe(list(one = 1, two = 2:3, three = 4:6, four = "four"))
#> # A tibble: 4 x 2
#> name value
#> <chr> <list>
#> 1 one <dbl [1]>
#> 2 two <int [2]>
#> 3 three <int [3]>
#> 4 four <chr [1]>
deframe()
is the opposite of enframe()