Learn R Programming

widyr (version 0.1.4)

widely: Adverb for functions that operate on matrices in "wide" format

Description

Modify a function in order to pre-cast the input into a wide matrix format, perform the function, and then re-tidy (e.g. melt) the output into a tidy table.

Usage

widely(.f, sort = FALSE, sparse = FALSE, maximum_size = 1e+07)

widely_(.f, sort = FALSE, sparse = FALSE, maximum_size = 1e+07)

Value

Returns a function that takes at least four arguments:

tbl

A table

row

Name of column to use as rows in wide matrix

column

Name of column to use as columns in wide matrix

value

Name of column to use as values in wide matrix

...

Arguments passed on to inner function

widely creates a function that takes those columns as bare names, widely_ a function that takes them as strings.

Arguments

.f

Function being wrapped

sort

Whether to sort in descending order of value

sparse

Whether to cast to a sparse matrix

maximum_size

To prevent crashing, a maximum size of a non-sparse matrix to be created. Set to NULL to allow any size matrix.

Examples

Run this code

library(dplyr)
library(gapminder)

gapminder

gapminder %>%
  widely(dist)(country, year, lifeExp)

# can perform within groups
closest_continent <- gapminder %>%
  group_by(continent) %>%
  widely(dist)(country, year, lifeExp)
closest_continent

# for example, find the closest pair in each
closest_continent %>%
  top_n(1, -value)

Run the code above in your browser using DataLab