Data processing with magclass
< Input data repositories & Patches | Overview | Input data preparation (MADRaT) >
Level: ⚫⚫⚫⚪⚪ Advanced
Requirements
Basic knowledge of R programming language
R installation
Content
Introduction to the magclass data class
magclass object functionality
data manipulation with magclass objects
Overview
magclass installation
The magclass
package is the data class most R functionality of MAgPIE is built on. Installing it is easy:
options(repos = c(CRAN = "https://cran.rstudio.com/",
pik = "https://rse.pik-potsdam.de/r/packages"))
install.packages("magclass")
library(magclass)
Setting the repos
option in the first line is optional and adds the PIK-hosted package repository as second repository after the official CRAN repository. Doing so will make sure that you receive the most up-to-date version of magclass
, without this line the package version currently hosted on CRAN will be installed, which is updated less regularly.
Vignettes
The package comes with some vignettes (tutorials) which introduce you to the world of magclass
and which also serve as basis for this tutorial. To list all tutorials run:
vignette(package = "magclass")
To open a vignette (e.g. the main vignette magclass
) just run:
vignette("magclass")
All vignettes are also hosted on the internet:
- magclass concept: Explaining the general concept behing magclass objects in comparison to arrays and data frames
- magclass main tutorial: Central tutorial explaining the basics how to use magclass
- magclass 6: Tutorial about some important changes which came with the complete rewrite of the package in version 6.
- magclass object expansion: more specific tutorial going into the details of object expansion and set matching.
Exercises (click on the arrows to uncover the solution)
Take the "animal" example data set from the magclass package and compute how many rabbits there are reported in Luxembourg (LUX) in may of each available year.
library(magclass)
a <- maxample("animal")
dimSums(a["LUX","may","rabbit"], dim = 3)
- Result is: 16 in 2000, 34 in 2001 and 100 in 2002
Write down three options to select the values for birds in Luxembourg (LUX) from the "animal" example data set.
library(magclass)
a <- maxample("animal")
a["LUX",,"bird"]
mselect(a, country = "LUX", species = "bird")
a[list(country = "LUX"),,list(species = "bird")]
- Bonus:
a["LUX", dim = 1]["bird", dim = 3]
Set all values for red birds in the animal data set to 0 and all values greater than 10 to 10.
library(magclass)
a <- maxample("animal")
a[,,"bird"][,,"red"] <- 0
a[a > 10] <- 10
Rename all dogs in the animal data set to cats and remove the "type" subdimension.
library(magclass)
a <- maxample("animal")
getItems(a, dim = "species")[3] <- "cat"
getItems(a, dim = "type") <- NULL
< Input data repositories & Patches | Overview | Input data preparation (MADRaT) >