-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
72 lines (55 loc) · 2.36 KB
/
README.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
---
title: "Bin cast"
author: "David Kaiser"
date: "14 May 2020"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
fig.align = "center",
comment = NULL
)
source("bin_cast.R")
```
## Description
This function is intended be used to "bin" CTD casts by depth. It takes a data frame and a bin depth that specifies the output depth interval. The column of depth values can by specified by name or index, otherwise the first column will be used. If a name is supplied, the column containing the depth bins will be called by this name, if no name or an index is supplid, the column will be called "binned_depth". A call to **aggregate()** uses this column for binning. The aggregation function can be set, it defaults to **mean()**. There's an option to output the bin size (number of values in each depth bin) with the result.
## Arguments
*x* -- dataframe containing the cast
*bin_depth* -- the interval the bins should take, e.g. *0.5* would round to the nearest 0.5 [in units of depth]
*depth_fun* = mean -- the function to be passed to aggregate, defaults to mean
*depth_col* = NULL -- name or index of the column containing the depth values, if not supplied first column will be used
*bin_size* = FALSE -- logical: should a column containing the bin size be attached to the output
## Result
A dataframe containing the binned results
## Examples
dummy data:
```{r dummy data}
cast <- data.frame(depth = sort(runif(100, min = 0, max = 100)),
temp = sort(runif(100, min = 20, max = 30), decreasing = TRUE),
sal = sort(runif(100, min = 18, max = 35)))
head(cast)
```
```{r plot original, echo=FALSE}
plot(cast$sal, cast$depth, ylim = rev(range(cast$depth)), xlab = "sal & T", ylab = "depth")
points(cast$temp, cast$depth, col="red")
```
binning data
```{r bin data}
binned_cast <- bin_cast(x = cast,
bin_depth = 10)
head(binned_cast)
```
```{r plot bin, echo=FALSE}
plot(binned_cast$sal, binned_cast[,1], ylim = rev(range(binned_cast[,1])), xlab = "sal & T", ylab = "depth")
points(binned_cast$temp, binned_cast[,1], col="red")
```
effects of arguments
```{r ex2}
binned_cast1 <- bin_cast(x = cast,
bin_depth = 2,
depth_col = "depth",
bin_fun = median,
bin_size = TRUE)
head(binned_cast1)
```