Skip to content

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.

Most dplyr verbs preserve row-wise grouping. The exception is summarise(), which return a grouped_df. You can explicitly ungroup with ungroup() or as_tibble(), or convert to a grouped_df with group_by().

Usage

rowwise(data, ...)

Arguments

data

Input data frame.

...

<tidy-select> Variables to be preserved when calling summarise(). This is typically a set of variables whose combination uniquely identify each row.

NB: unlike group_by() you can not create new variables here but instead you can select multiple variables with (e.g.) everything().

Value

A row-wise data frame with class rowwise_df. Note that a rowwise_df is implicitly grouped by row, but is not a grouped_df.

List-columns

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

See also

nest_by() for a convenient way of creating rowwise data frames with nested data.

Examples

df <- tibble(x = runif(6), y = runif(6), z = runif(6))
# Compute the mean of x, y, z in each row
df %>% rowwise() %>% mutate(m = mean(c(x, y, z)))
#> # A tibble: 6 × 4
#> # Rowwise: 
#>       x      y     z     m
#>   <dbl>  <dbl> <dbl> <dbl>
#> 1 0.386 0.929  0.723 0.679
#> 2 0.237 0.645  0.696 0.526
#> 3 0.520 0.783  0.871 0.724
#> 4 0.641 0.0587 0.709 0.470
#> 5 0.830 0.358  0.355 0.514
#> 6 0.919 0.479  0.807 0.735
# use c_across() to more easily select many variables
df %>% rowwise() %>% mutate(m = mean(c_across(x:z)))
#> # A tibble: 6 × 4
#> # Rowwise: 
#>       x      y     z     m
#>   <dbl>  <dbl> <dbl> <dbl>
#> 1 0.386 0.929  0.723 0.679
#> 2 0.237 0.645  0.696 0.526
#> 3 0.520 0.783  0.871 0.724
#> 4 0.641 0.0587 0.709 0.470
#> 5 0.830 0.358  0.355 0.514
#> 6 0.919 0.479  0.807 0.735

# Compute the minimum of x and y in each row
df %>% rowwise() %>% mutate(m = min(c(x, y, z)))
#> # A tibble: 6 × 4
#> # Rowwise: 
#>       x      y     z      m
#>   <dbl>  <dbl> <dbl>  <dbl>
#> 1 0.386 0.929  0.723 0.386 
#> 2 0.237 0.645  0.696 0.237 
#> 3 0.520 0.783  0.871 0.520 
#> 4 0.641 0.0587 0.709 0.0587
#> 5 0.830 0.358  0.355 0.355 
#> 6 0.919 0.479  0.807 0.479 
# In this case you can use an existing vectorised function:
df %>% mutate(m = pmin(x, y, z))
#> # A tibble: 6 × 4
#>       x      y     z      m
#>   <dbl>  <dbl> <dbl>  <dbl>
#> 1 0.386 0.929  0.723 0.386 
#> 2 0.237 0.645  0.696 0.237 
#> 3 0.520 0.783  0.871 0.520 
#> 4 0.641 0.0587 0.709 0.0587
#> 5 0.830 0.358  0.355 0.355 
#> 6 0.919 0.479  0.807 0.479 
# 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 computation
params %>%
  rowwise(sim) %>%
  reframe(z = rnorm(n, mean, sd))
#> # A tibble: 6 × 2
#>     sim       z
#>   <dbl>   <dbl>
#> 1     1  0.622 
#> 2     2  7.32  
#> 3     2 -1.09  
#> 4     3  0.0376
#> 5     3 -0.787 
#> 6     3  0.163 

# If you want one row per simulation, put the results in a list()
params %>%
  rowwise(sim) %>%
  summarise(z = list(rnorm(n, mean, sd)), .groups = "keep")
#> # A tibble: 3 × 2
#> # Groups:   sim [3]
#>     sim z        
#>   <dbl> <list>   
#> 1     1 <dbl [1]>
#> 2     2 <dbl [2]>
#> 3     3 <dbl [3]>