https://github.com/hadley/dplyr
Raw File
Tip revision: 39ee11bfe78c4a301070b00d3b92127217786ba7 authored by Hadley Wickham on 23 January 2020, 16:18:08 UTC
Preserve drop attr
Tip revision: 39ee11b
rowwise.Rd
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rowwise.r
\name{rowwise}
\alias{rowwise}
\title{Group input by rows}
\usage{
rowwise(data, ...)
}
\arguments{
\item{data}{Input data frame.}

\item{...}{<\code{\link[=dplyr_tidy_select]{tidy-select}}> Variables to be preserved
when calling \code{\link[=summarise]{summarise()}}. This is typically a set of variables whose
combination uniquely identify each row.

\strong{NB}: unlike \code{group_by()} you can not create new variables here but
instead you can select multiple variables with (e.g.) \code{everything()}.}
}
\description{
\code{rowwise()} allows you to compute on a data frame a row-at-a-time.
This is most useful when a vectorised function doesn't exist.

A row-wise tibble maintains its row-wise status until explicitly removed
by \code{\link[=group_by]{group_by()}}, \code{\link[=ungroup]{ungroup()}}, or \code{\link[=as_tibble]{as_tibble()}}.
}
\section{List-columns}{

Because a rowwise has exactly one row per group it offers a small
convenience for working with list-columns. Normally, \code{summarise()} and
\code{mutate()} extract a groups worth of data with \code{[}. But when you index
a list in this way, you get back another list. When you're working with
a \code{rowwise} tibble, then dplyr will use \code{[[} instead of \code{[} to make your
life a little easier.
}

\examples{
df <- tibble(x = runif(6), y = runif(6))
# Compute the mean of x and y in each row
df \%>\% rowwise() \%>\% mutate(z = mean(c(x, y)))

# Compute the minimum of x and y in each row
df \%>\% rowwise() \%>\% mutate(z = min(c(x, y)))
# In this case you can use an existing vectorised function:
df \%>\% mutate(z = pmin(x, y))
# Where these functions exist they'll be much faster than rowwise
# so be on the lookout for them.

# rowwise() is also useful when doing simulations
params <- tribble(
 ~sim, ~n, ~mean, ~sd,
    1,  1,     1,   1,
    2,  2,     2,   4,
    3,  3,    -1,   2
)
# Here I supply variables to preserve after the summary
params \%>\%
  rowwise(sim) \%>\%
  summarise(z = rnorm(n, mean, sd))

# If you want one row per simulation, put the results in a list()
params \%>\%
  rowwise(sim) \%>\%
  summarise(z = list(rnorm(n, mean, sd)))

# Or use do() which do this automatically:
params \%>\%
  rowwise(sim) \%>\%
  condense(z = rnorm(n, mean, sd))
}
back to top