There are various ways to create tibbles. Below are three methods:
Creating a new tibble via code
Coercing a data.frame into a tibble
Reading in a file as a tibble
Code to tibble
When creating a tibble through code you provide the tibble::tibble() function with vectors of identical length. Each vector will become a column.
#Vectors to be used as columnsDay <-c("Mon", "Tue", "Wed")Month <-rep("January", 3)Hawks <-c(3,7,2)Pigeons <-c(52,21,61)Starlings <-c(1,2,1)#Create a tibbletbl <- tibble::tibble(Day, Month, Hawks, Pigeons, Starlings)#View tibble#Note that the column names are the same as the vector namestbl
# A tibble: 3 × 5
Day Month Hawks Pigeons Starlings
<chr> <chr> <dbl> <dbl> <dbl>
1 Mon January 3 52 1
2 Tue January 7 21 2
3 Wed January 2 61 1
data.frame to tibble
Let’s load in the built-in iris data frame and convert it to a tibble.
Rows: 155 Columns: 33
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): common_name
dbl (32): id, seeds, drought_tolerant, salt_tolerant, thorny, invasive, trop...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.