help frapply https://github.com/gaksaray/stata-gautils/

Title

frapply -- Nondestructively apply command(s) to a frame

Syntax

frapply [framename1]
[if] [in]
[,
into(framename2 [, replacechange])
quietly]
[: commandlist]

where the syntax of commandlist is

command [ |> command [ |> command [...]]]

and command is any Stata command.

frapply is a flexible command.
It takes framename1 (or current frame if not specified),
subsets it by if and in,
applies daisy chained command(s) to it,
and puts the end result in either a new frame, an existing frame if replace is specified, or a temporary frame (discarded when the program concludes) if into() is not specified at all.

framename1 (or current frame) is always preserved.

Note:
Specifying framename1 inside frapply is functionally equivalent to using frame prefix as in frame framename1: frapply.
However, frame: prefix is not recommended to use with frapply not only because it is redundant but also because it disables the change option (see options).

Description

frapply applies a command or a series of commands to the dataset in the specified (or current) frame and optionally puts the result into another frame.
Commands that are otherwise destructive (such as drop, keep, collapse, contract etc.) can be run serially while preserving the dataset.
This can be particularly useful in interactive and experimental settings where we want to quickly and iteratively summarize and/or transform the data without changing it.

frapply can also be a convenient drop-in replacement for the frames prefix and a substitute for frames commands such as frame copy and frame put.
It can do everything those commands can do in a more flexible fashion.

Options

replace overwrites the framename2.

change makes the framename2 current frame.

quietly silences the output of frapply. Use noisily to display the results of any command in commandlist.

Examples

Running frapply on multiple datasets

. frames reset

. frame create auto1

. frame create auto2

. frame auto1: sysuse auto

. frame auto2: sysuse auto2

. frapply auto1, into(copy1, replace): collapse mpg, by(foreign)

. cwf copy1 // includes summary stats; auto1 is not changed

. frapply auto2, into(copy2, replace): contract mpg foreign

. cwf copy2 // includes frequencies; auto2 is not changed

Applying serial commands interactively

. sysuse auto, clear

. frapply if price > 5000 & mpg > 15, into(subset)

. frapply, into(subset, replace change): ///

keep if price > 5000 |> /// you may change these numbers

keep if mpg > 15 |> /// and run frapply again

collapse price mpg, by(foreign) // to see the result change

Listing means by group

. sysuse auto, clear

. frapply: collapse price mpg, by(foreign) |> list

Displaying predictive margins

. sysuse auto, clear

. frapply, qui: reg price mpg |> predict yhat |> collapse yhat |> noi l

Remarks

frapply uses |>-separator to daisy chain commands, somewhat similar to the pipe operator in R (and in Tidyverse).
This was changed from an earlier version of frapply in which the separator was ||.
This is in order to distinguish it from the ||-separator notation used in twoway, which denotes serial superimposition rather than chaining.
The |> operator makes it clear that it acts as a forward pipe, and is also more familiar for folks coming from R.

One seeming limitation of using daisy-chained commandlist is that macros produced by a command may not be an input of a subsequent command.
For example, summary |> di `r(mean)' would summarize the dataset but not display r(mean), as Stata would substitute the local macro before frapply starts to run.

To illustrate,

. sysuse auto, clear

. frapply, qui: sum price |> noi di r(mean)

displays the mean. However,

. frapply, qui: sum price |> noi di `r(mean)'

doesn't display the mean, as the local macro `r(mean)' is substituted to nothing right before frapply runs.

This can be circumvented by using escape character "\" before any macro.
In the same example,

. frapply, qui: sum price |> noi di \`r(mean)'

does display the mean.

Stored results

frapply returns all r-class and e-class results stored by the last command in commandlist.

Author

Gorkem Aksaray, Trinity College Dublin.

Email: aksarayg@tcd.ie

Personal Website: sites.google.com/site/gorkemak

GitHub: github.com/gaksaray