Skip to content

Commit

Permalink
update attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
JSBoey committed Apr 8, 2024
1 parent 3885b14 commit 12c2a91
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions docs/5.data_structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<center>

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 12c2a91

Please sign in to comment.