-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrangleMD.Rmd
98 lines (71 loc) · 2.98 KB
/
wrangleMD.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
---
title: "Data School Activity 1 - Small Data"
author: "Mark Farrell"
date: "08/08/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Impact of biostimulants on soil nitrogen dynamics
## Overview
These data are from a pot experiment that was conducted to investigate whether biostimulants impact on soil nitrogen (N) cycling and wheat uptake of legume-derived N. Here, we are looking at soluble N and carbon (C) pools over the growth period. These are presented in mg L^-1^ as they are derived from soil solution samples that were collected non-destructively using [Rhizon samplers](https://www.rhizosphere.com/).
![](https://www.rhizosphere.com/assets/images/RhizonCSSinacoreRRP.jpg)
## Initial data processing, tidying, etc
First, we need to lead tidyverse, and bring the data into the R environment
```{r load_libraries, message=FALSE}
library(tidyverse)
raw_data <- read_csv("results/raw/15n_soil_sol.csv")
```
We can now see that the data has been successfully loaded into the environment:
```{r raw}
raw_data
```
There are now several steps that need to be taken before we can start graphing the data. Some variables need to be calculated:
* DIN
* DON
* DOC_DON ratio
* DON_DIN ratio
Making a new variable is easy. As I forgot to give it a name, I also re-named is using the ```name``` function in base R
```{r DIN, message=FALSE}
with_din <- mutate(raw_data, nh4 + no3)
names(with_din)[11] <- "din"
```
As part of this there is some cleaning that also needs to happen. In calculating DON, we can use ```case_when()``` in the mutate function to identify values < 0 and make them 0:
```{r mutations, message=FALSE}
with_don <- with_din %>%
mutate(don = tdn - din,
don_nz = case_when(
don >= 0 ~ don,
TRUE ~ 0
))
```
```{r hidden 1, echo=FALSE, message=FALSE, results=FALSE}
##do we have any negatives?
count(with_don, don_nz < 0)
##tidying...
don_fixed <- select(with_don, -don)
names(don_fixed)[12] <- "don"
```
Doing some more simple mutations calculates the ratios, but also generates ```Inf``` where a divide by zero has happened
```{r ratios_calc}
ratios <- mutate(don_fixed, doc_don = doc / don )
ratios <- mutate(ratios, don_din = don / din )
```
```{r ratios, echo=FALSE}
ratios
```
The ```case_when``` operation above won't work here straight away to turn ```Inf``` to ```'NA'```
So, Alex provided a solution using <mutate> to change all infinate values to NA:
```don_fixed$doc_don[which(is.infinite(don_fixed$doc_don))] <- NA
```
I also found one on [Stack Overflow](https://stackoverflow.com/questions/12188509/cleaning-inf-values-from-an-r-dataframe)
```{r kill_inf, message=FALSE}
is.na(ratios) <- do.call(cbind,lapply(ratios, is.infinite))
```
This searches the data frame, finds infinate values and replaces them with NA
```{r, echo=FALSE}
ratios
```
Finally, the finished dataframe is written to a new *.csv
```write_csv(ratios, "results/processed/ratios.csv") # DON'T FORGET THE QUOTATIONS ON THE PATH!!!!```