Tables in R - Data frames and tibbles
Slides in full screen Download PDF slides
1 Task
Note
Find the solution here.
1.1 Get started with tibbles
Before you start the task:
- Install the tibble package by calling
install.packages("tibble")
in the console.- You don’t need to put this command in your script as you only need to call it once to install the package
- Open your script for the task and put
library(tibble)
on top - Execute this line and the tibble package is loaded into your R session
1.2 Create a tibble
Use the vectors species
, brainwt_kg
, bodywt_kg
from before and combine them in a tibble.
1.3 Explore the tibble
Explore the tibble you created with the different functions that you learned in the presentation. You can for example try
view()
and explore what you can do in the view window that opens: searching, filtering, sorting columnssummary()
- …
1.4 Index, filter and select
Now let’s try some indexing on the data.
- Rows 1, 5, and 7 and the columns
species
andbodywt_kg
- Filter rows 1 to 10, all columns
- Select the column
bodywt_kg
as a vector
1.5 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)
- Calculate the mean of the
bodywt_kg
column and save it in a variable - Add a new column to the data called
ratio_body_brain
with the ratio of the columnsbodywt_kg
andbrainwt_kg