From 12c2a913073f03fcdc0fc7c8ff3b746d4046e668 Mon Sep 17 00:00:00 2001 From: Jian Sheng Boey <95262076+JSBoey@users.noreply.github.com> Date: Mon, 8 Apr 2024 13:44:04 +1200 Subject: [PATCH] update attributes --- docs/5.data_structures.md | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/5.data_structures.md b/docs/5.data_structures.md index 7180f85..be89ffa 100644 --- a/docs/5.data_structures.md +++ b/docs/5.data_structures.md @@ -2,11 +2,10 @@ !!! note "Learning objectives" - * Understand data frames are special lists + * Understand data frames and tibbles are special lists * Know when and why to use a matrix - * * Understand attributes and how they affect data structure - * Use tibbles for data storage + * Understand recursion in functions
@@ -194,7 +193,7 @@ The matrix is another class of tabular data object. They are the 2-dimensional g === "Strict subsetting" - When using square bracket notation for subsetting matrices, the result is always an atomic vector if there is only one row/column, or a matrix if more then 1 row/column is desired. + When using square bracket notation for subsetting matrices, the result is always an atomic vector if there is only one row OR column, or a matrix if more then 1 row AND column is desired. !!! r-project "code" @@ -259,7 +258,33 @@ The matrix is another class of tabular data object. They are the 2-dimensional g ### Attributes -Think of attributes as metadata to the object. Some attributes determine the structure of the data so that R can interpret them properly, other attributes are attached as metadata or for the purposes of provenance. Every time we perform some action on an object, the object's class attribute will be examined prior to evaluation as some operations are restricted to certain classes. Besides `class()`, other basic attributes include `names()`, dimensions `dim()`, and dimension names `dimnames()`. In the context of matrices, `dim()` and `dimnames()` (list of row and column names, in that specific order) are important attributes that differentiates it from a 1-dimensional atomic vector. +Think of attributes as metadata to the object. Some attributes determine the structure of the data so that R can interpret them properly, other attributes are attached as metadata or for the purposes of provenance. Every time we perform some action on an object, the object's class attribute will be examined prior to evaluation as some operations are restricted to certain classes. Besides `class()`, other basic attributes include `names()`, dimensions `dim()`, and dimension names `dimnames()`. In the context of matrices, `dim()` and `dimnames()` (list of row and column names, in that specific order) are important attributes that differentiates it from a 1-dimensional atomic vector. + +To extract and modify attributes: + +!!! r-project "code" + + ```r linenums="1" + # Create example data + env_scaled <- scale(env_matrix) + + # Inspect attributes + attributes(env_scaled) + + # Extract attributes + attributes(env_scaled)$`scaled:scale` + ``` + +As observed, calling `attributes()` returns a list object of the available attributes from which we can inspect. As it is a list, we can modify and add attributes by assigning it like a list-element. + +!!! r-project "code" + + ```r linenums="1" + attributes(env_scaled)$method <- "Z-score standardisation" + + # Inspect attributes + attributes(env_scaled) + ``` ## Recursive objects: Functions