Task: Data frames and tibbles
Find the solution here after the session ends.
Get started with tibbles
Before you start the task:
- If you haven’t already, install the tibble package by calling
install.packages("tibble")in the console.
Don’t put
install.packages() in your script
You don’t need to put install.packages("tibble") in your script. You only need to install a package once on your computer, not every time you run the script. So you can just run this line once in the console.
- 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
Create a tibble
Use the vectors species, brainwt_kg, bodywt_kg from the previous task and combine them in a tibble.
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()- …
Index, filter and select
Now let’s try some indexing on the data.
- Rows 1, 5, and 7 and the columns
speciesandbodywt_kg - Filter rows 1 to 10, all columns
- Select the column
bodywt_kgas a vector
Optional tasks (if you finish early)
You can do these in any order — or skip them and just take a break.
- 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_kgcolumn and save it in a variable - Add a new column to the data called
ratio_body_brainwith the ratio of the columnsbodywt_kgandbrainwt_kg