Solution to tibble tasks

1 Create a tibble

Use the vectors species, brainwt_kg, bodywt_kg from the last task and put them in a tibble.

# install.packages("tibble")
library(tibble)

species <- c(
  "MountainBeaver", "Cow", "GreyWolf", "Goat",
  "GuineaPig", "Diplodocus", "AsianElephant", "Donkey",
  "Horse", "PotarMonkey", "Cat", "Giraffe",
  "Gorilla", "Human", "AfricanElephant", "Triceratops",
  "RhesusMonkey", "Kangaroo", "GoldenHamster", "Mouse",
  "Rabbit", "Sheep", "Jaguar", "Chimpanzee",
  "Rat", "Brachiosaurus", "Mole", "Pig"
)

bodywt_kg <- c(
  1.4, 465, 36.3, 27.7, 1., 11700, 2547, 187.1,
  521, 10, 3.3, 529, 207, 62, 6654, 9400,
  6.8, 35, 0.1, 0.02, 2.5, 55.5, 100, 52.2,
  0.3, 87000, 0.1, 192
)

brainwt_kg <- c(
  0.0081, 0.423, 0.1195, 0.115, 0.0055, 0.05, 
  4.603, 0.419, 0.655, 0.115, 0.0256, 0.68, 
  0.406, 1.32, 5.712, 0.07, 0.179, 0.056,
  0.001, 0.0004, 0.0121, 0.175, 0.157, 0.44,
  0.0019, 0.1545, 0.003, 0.18
)

# Create the tibble 
animals <- tibble(
  species = species,
  bodywt_kg = bodywt_kg,
  brainwt_kg = brainwt_kg
)

2 Explore the tibble

When you use the view function, the tibble will open in a new window. In this window, you also have some options to filter the data and sort rows. Check out these options as well.

view(animals)

Print a summary of the table:

summary(animals)
   species            bodywt_kg          brainwt_kg     
 Length:28          Min.   :    0.02   Min.   :0.00040  
 Class :character   1st Qu.:    3.10   1st Qu.:0.02223  
 Mode  :character   Median :   53.85   Median :0.13700  
                    Mean   : 4278.44   Mean   :0.57452  
                    3rd Qu.:  479.00   3rd Qu.:0.42000  
                    Max.   :87000.00   Max.   :5.71200  

Here a couple of other function you could use on a tibble:

# Look at the structure of the tibble
str(animals)
tibble [28 × 3] (S3: tbl_df/tbl/data.frame)
 $ species   : chr [1:28] "MountainBeaver" "Cow" "GreyWolf" "Goat" ...
 $ bodywt_kg : num [1:28] 1.4 465 36.3 27.7 1 ...
 $ brainwt_kg: num [1:28] 0.0081 0.423 0.1195 0.115 0.0055 ...
# Number of rows
nrow(animals)
[1] 28
# Number of columns
ncol(animals)
[1] 3
# Column names
names(animals)
[1] "species"    "bodywt_kg"  "brainwt_kg"

3 Index, filter and select

  • Rows 1, 5, and 7 and the columns species and bodywt_kg
animals[c(1,5,7), c("species", "bodywt_kg")]
# A tibble: 3 × 2
  species        bodywt_kg
  <chr>              <dbl>
1 MountainBeaver       1.4
2 GuineaPig            1  
3 AsianElephant     2547  
  • Filter rows 1 to 10, all columns
animals[1:10, ]
# A tibble: 10 × 3
   species        bodywt_kg brainwt_kg
   <chr>              <dbl>      <dbl>
 1 MountainBeaver       1.4     0.0081
 2 Cow                465       0.423 
 3 GreyWolf            36.3     0.120 
 4 Goat                27.7     0.115 
 5 GuineaPig            1       0.0055
 6 Diplodocus       11700       0.05  
 7 AsianElephant     2547       4.60  
 8 Donkey             187.      0.419 
 9 Horse              521       0.655 
10 PotarMonkey         10       0.115 
  • Select the column bodywt_kg as a vector using $
animals$bodywt_kg
 [1]     1.40   465.00    36.30    27.70     1.00 11700.00  2547.00   187.10
 [9]   521.00    10.00     3.30   529.00   207.00    62.00  6654.00  9400.00
[17]     6.80    35.00     0.10     0.02     2.50    55.50   100.00    52.20
[25]     0.30 87000.00     0.10   192.00

4 Extra

  • Select the last column of the last row without using the row and column index (so don’t use any numbers in this command)
animals[nrow(animals), ncol(animals)]
# A tibble: 1 × 1
  brainwt_kg
       <dbl>
1       0.18
  • Calculate the mean of the bodywt_kg column and save it in a variable
mean_wt <- mean(animals$bodywt_kg)
mean_wt
[1] 4278.44
  • Add a new column to the data called ratio_body_brain with the ratio of the columns bodywt_kg and brainwt_kg
animals$ratio_body_brain <- animals$bodywt_kg / animals$brainwt_kg
animals
# A tibble: 28 × 4
   species        bodywt_kg brainwt_kg ratio_body_brain
   <chr>              <dbl>      <dbl>            <dbl>
 1 MountainBeaver       1.4     0.0081            173. 
 2 Cow                465       0.423            1099. 
 3 GreyWolf            36.3     0.120             304. 
 4 Goat                27.7     0.115             241. 
 5 GuineaPig            1       0.0055            182. 
 6 Diplodocus       11700       0.05           234000  
 7 AsianElephant     2547       4.60              553. 
 8 Donkey             187.      0.419             447. 
 9 Horse              521       0.655             795. 
10 PotarMonkey         10       0.115              87.0
# ℹ 18 more rows