---
date: "`r Sys.Date()`"
title: "Extra functions in manymodelr"
output: html_document
vignette: >
  %\VignetteIndexEntry{ "Extra functions in manymodelr"}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```



```{r}
library(manymodelr)
```


- **`agg_by_group`**

As can be guessed from the name, this function provides an easy way to manipulate grouped data. We can for instance find the number of observations in the yields data set. The formula takes the form `x~y` where `y` is the grouping variable(in this case `normal`). One can supply a formula as shown next. 

```{r}
# Load the yields dataset
data("yields")
head(agg_by_group(yields,.~normal,length))

```

```{r}

head(agg_by_group(mtcars,cyl~hp+vs,sum))

```


- `rowdiff`

 This is useful when trying to find differences between rows. The `direction` argument specifies how the subtractions are made while the `exclude` argument is used to specify classes that should be removed before calculations are made.  Using `direction="reverse"` performs a subtraction akin to `x-(x-1)` where `x` is the row number. 

```{r}

head(rowdiff(yields,exclude = "factor",direction = "reverse"))

```


- `na_replace`

This allows the user to conveniently replace missing values. Current options are `ffill` which replaces with the next non-missing value, `samples` that samples the data and does replacement, `value` that allows one to fill `NA`s with a specific value. Other common mathematical methods like `min`, `max`,`get_mode`, `sd`, etc are no longer supported. They are now available with more flexibility in standalone [mde](https://github.com/Nelson-Gon/mde)

```{r}

head(na_replace(airquality, how="value", value="Missing"),8)

```

- `na_replace_grouped`

This provides a convenient way to replace values by group.
```{r}
test_df <- data.frame(A=c(NA,1,2,3), B=c(1,5,6,NA),groups=c("A","A","B","B"))
# Replace NAs by group
# replace with the next non NA by group.
na_replace_grouped(df=test_df,group_by_cols = "groups",how="ffill")

```

The use of `mean`,`sd`,etc is no longer supported. Use [mde](https://github.com/Nelson-Gon/mde) instead which is focused on missingness. 

