-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectors-vs-structures.Rmd
110 lines (81 loc) · 4.85 KB
/
vectors-vs-structures.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
title: "Vectors and scalars versus structures"
author: "tagtools project team"
date: "`r Sys.Date()`"
output:
html_document:
code_folding: hide
vignette: >
%\VignetteIndexEntry{vectors-vs-structures}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
require(tagtools)
```
Welcome to the `vectors-vs-structures` vignette! Thanks for taking some time to get to know our package; we hope it is useful to you. In this quick vignette, you will distinguish between vectors and structures.
*Estimated time for this practical: 15 minutes.*
These vignettes assume that you have R/Rstudio installed on your machine, some basic experience working with them, and can execute provided code, making some user-specific changes along the way (e.g. to help R find a file you downloaded). We will provide you with quite a few lines. To boost your own learning, you would do well to try and write them before opening what we give, using this just to check your work.
# Setup: write "cats_test_raw.nc", and load it as a large list, `MN`
For this vignette we will use the NetCDF file `cats_test_raw.nc`. If you want to run this example, download the "cats_test_raw.nc" file from https://github.com/animaltags/tagtools_data and change the file path to match where you've saved the files
```{r}
library(tagtools)
cats_file_path <- "nc_files/cats_test_raw.nc"
MN <- load_nc(cats_file_path)
```
This will write the file "cats_test_raw.nc", and load it into your current R session as a list object called `MN`. Looks good? Good work! Confused? See the vignette `load-tag-data`.
# Sensor data structures & extracting data vectors
Having caught up, we return to sensor data structures. Like `$info`:
```{r, echo = TRUE, eval = FALSE}
MN$info
```
<button class="btn btn-primary" data-toggle="collapse" data-target="#MN_info"> Show/Hide Results </button>
<div id="MN_info" class="collapse">
```{r, echo = FALSE, eval = TRUE}
MN$info
```
</div>
these are structures that contain both the data and some information about it. Typing any of the variable names followed by enter will show what is in it. For example, try this for Acceleration:
```{r, echo = TRUE, eval = TRUE}
str(MN$A, max.level = 1) # display the STRucture of MN$A
```
In Matlab, Octave and R it will look a little different, but will have all the same information. Specifically, we are after the variable `A`, which is contained within the large list MN. `MN$A` is how we access it, in R.
The results tell you that the acceleration data were sampled at a nominal rate of 32 Hz (32 samples per second) and are in units of meters per second squared. If you are more familiar with data vectors than with structures, you can easily get the data out of the structure, and into its own standalone object, using:
```{r, echo = TRUE, eval = FALSE}
A <- MN$A$data
sampling_rate <- MN$A$sampling_rate
str(A, max.level = 1)
sampling_rate
```
<button class="btn btn-primary" data-toggle="collapse" data-target="#data_out"> Show/Hide Results </button>
<div id="data_out" class="collapse">
```{r, echo = FALSE, eval = TRUE}
A <- MN$A$data
sampling_rate <- MN$A$sampling_rate
print("Results for str(A, max.level = 1):")
print("----------------------------------")
str(A, max.level = 1)
print("----------------------------------")
print("Result for sampling_rate:")
print("----------------------------------")
sampling_rate
```
</div>
This makes a matrix A with the acceleration data and a scalar `sampling_rate` with the sampling rate, which in this case is 32. You can also just keep everything stored in the list object, if you like (in R there is not a clear reason to pull them out). If you do this, you'd just have to keep typing `MN$A$data` and `MN$A$sampling_rate` instead of `A` or `sampling_rate`. If you move forward to `plots-and-cropping`, you will still use the animaltag list `MN`, pulling data directly from it.
# Review
You've learned to distinguish between vectors and structures, and go from a structure to a vector (or scalar).
Nice work! You're all set on `vectors-vs-structures`.
*If you'd like to keep working through these practicals, `plots-and-cropping` is a logical continuation. It's particularly nice to learn to use the plotter that comes with tagtools, called `plott`.*
```{r, echo = TRUE, eval = FALSE}
vignette('plots-and-cropping', package = 'tagtools')
```
*Otherwise, you might consider jumping ahead to `data-quality-error-correction`. In it, you'll get started with the work of fixing errors in data.*
```{r, echo = TRUE, eval = FALSE}
vignette('data-quality-error-correction', package = 'tagtools')
```
***
Animaltags home pages: http://animaltags.org/ (old), https://animaltags.netlify.app/ (new), https://github.com/stacyderuiter/TagTools (for latest beta source code), https://stacyderuiter.github.io/TagTools/articles/TagTools (vignettes overview)