tibble::tibble()
and tibble::tribble()
get_help()
docs
The tibble()
and tribble()
functions are part of the {tibble}
package, which is part of the {tidyverse}
.
We use the functions tibble()
and tribble()
functions to quickly create tibbles from scratch within R. You can use either function depending on your personal preference.
To use these functions, you need to either first load the {tibble}
library, or always use the function with tibble::
notation.
# Load the library
library(tibble)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::tibble()
tibble::tribble() tibble
# We give arrays to the tibble() function.
# Arrays should be of the same length and will become the columns
tibble(column_name1 = values for column 1,
column_name2 = values for column 2,
etc.)
# We use special syntax for tribble that writes the data "appearing like a table"
# The tildes `~` are used to indicate column names
tribble(
~column_name1, ~column_name2, ~etc for more columns,
first row of column1, first row of column2, etc.,
second row of column1, second row of column2, etc. )
# The examples below use tibble::tibble() and tibble::tribble to recreate this tibble:
example_tibble
## # A tibble: 3 × 3
## food color food_type
## <chr> <chr> <chr>
## 1 cucumber green fruit
## 2 pomegranate red fruit
## 3 carrot orange root
# Re-create example_tibble with tibble::tibble()
tibble(
food = c("cucumber", "pomegranate", "carrot"),
color = c("green", "red", "orange"),
food_type = c("fruit", "fruit", "root")
)
## # A tibble: 3 × 3
## food color food_type
## <chr> <chr> <chr>
## 1 cucumber green fruit
## 2 pomegranate red fruit
## 3 carrot orange root
# Re-create example_tibble with tibble::tribble()
# Use *spacing* to help guide yourself
tribble(
~food, ~color, ~food_type,
"cucumber", "green", "fruit",
"pomegranate", "red", "fruit",
"carrot", "orange", "root")
## # A tibble: 3 × 3
## food color food_type
## <chr> <chr> <chr>
## 1 cucumber green fruit
## 2 pomegranate red fruit
## 3 carrot orange root