knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(manymodelr)

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.

# Load the yields dataset
data("yields")
head(agg_by_group(yields,.~normal,length))
head(agg_by_group(mtcars,cyl~hp+vs,sum))

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.

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

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 NAs 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

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

This provides a convenient way to replace values by group.

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 instead which is focused on missingness.



Try the manymodelr package in your browser

Any scripts or data that you put into this service are public.

manymodelr documentation built on Nov. 15, 2021, 5:07 p.m.