library(tidyverse)
####创建tibble
as_tibble(iris) #将数据框转换为tibble
tibble(
x = 1:5,
y = 1,
z = x ^ 2 + y
) #使用tibble()将一个向量创建为tibble
tribble(
~x, ~y, ~z,
#--|--|----
"a", 2, 3.6,
"b", 1, 8.5
)
####打印
tibble(
a = lubridate::now() + runif(1e3) * 86400, b = lubridate::today() + runif(1e3) * 30, c = 1:1e3,
d = runif(1e3),
e = sample(letters, 1e3, replace = TRUE)
)
nycflights13::flights %>%
print(n = 10, width = Inf) #显示10行所有列
####与旧代码进行交互
#如果比较旧的函数不支持tibble,可以用as.data.frame()将tibble转换为数据框
(tb <- tibble(
x = 1:5,
y = 1,
z = x ^ 2 + y
))
class(as.data.frame(tb))