Creating tibbles

There are various ways to create tibbles. Below are three methods:

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 columns
Day <- 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 tibble
tbl <- tibble::tibble(Day, Month, Hawks, Pigeons, Starlings)
#View tibble
#Note that the column names are the same as the vector names
tbl
# 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.

#Head of iris data.frame
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          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.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
#Coerce to tibble
iris_tbl <- tibble::as_tibble(iris)
#View iris tibble
iris_tbl
# A tibble: 150 × 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 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# ℹ 140 more rows

Note: The iris dataset is built-in to base R. You can see all the pre-loaded datasets in R with data()

File to tibble

We can use readr’s function read_csv() to read in a file as a tibble.

More information: readr

Ensure you have the following file downloaded:

all_plants_details.csv download

house_plants_tbl <- readr::read_csv(file = "all_plant_details.csv")
house_plants_tbl
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.
# A tibble: 155 × 33
      id common_name        seeds drought_tolerant salt_tolerant thorny invasive
   <dbl> <chr>              <dbl>            <dbl>         <dbl>  <dbl>    <dbl>
 1   425 flowering-maple        0                1             0      1        0
 2   426 flowering-maple        0                1             0      0        0
 3   427 flowering-maple        0                1             0      0        0
 4   428 flowering-maple        0                1             1      0        0
 5   434 Jacob's coat           0                0             0      0        0
 6   502 hot water plant        0                0             0      0        0
 7   540 desert rose            0                1             1      1        0
 8   543 maidenhair fern        0                0             0      0        0
 9   546 delta maidenhair …     0                1             0      0        0
10   549 urn plant              1                1             1      0        0
# ℹ 145 more rows
# ℹ 26 more variables: tropical <dbl>, indoor <dbl>, flowers <dbl>,
#   cones <dbl>, fruits <dbl>, edible_fruit <dbl>, leaf <dbl>,
#   edible_leaf <dbl>, cuisine <dbl>, medicinal <dbl>,
#   poisonous_to_humans <dbl>, poisonous_to_pets <dbl>,
#   sunlight_part_sun_part_shade <dbl>, sunlight_full_shade <dbl>,
#   sunlight_deep_shade <dbl>, sunlight_part_shade <dbl>, …