rowwise: Group input by rows

Description Usage Arguments Value List-columns See Also Examples

View source: R/rowwise.r

Description

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

1

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)))
# use c_across() to more easily select many variables
df %>% rowwise() %>% mutate(m = mean(c_across(x:z)))

# Compute the minimum of x and y in each row
df %>% rowwise() %>% mutate(m = min(c(x, y, z)))
# In this case you can use an existing vectorised function:
df %>% mutate(m = pmin(x, y, z))
# 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)))

Example output

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

# A tibble: 6 x 4
# Rowwise: 
       x     y      z     m
   <dbl> <dbl>  <dbl> <dbl>
1 0.662  0.423 0.793  0.626
2 0.0925 0.164 0.114  0.124
3 0.643  0.495 0.0172 0.385
4 0.659  0.708 0.706  0.691
5 0.692  0.812 0.0128 0.506
6 0.166  0.417 0.889  0.491
# A tibble: 6 x 4
# Rowwise: 
       x     y      z     m
   <dbl> <dbl>  <dbl> <dbl>
1 0.662  0.423 0.793  0.626
2 0.0925 0.164 0.114  0.124
3 0.643  0.495 0.0172 0.385
4 0.659  0.708 0.706  0.691
5 0.692  0.812 0.0128 0.506
6 0.166  0.417 0.889  0.491
# A tibble: 6 x 4
# Rowwise: 
       x     y      z      m
   <dbl> <dbl>  <dbl>  <dbl>
1 0.662  0.423 0.793  0.423 
2 0.0925 0.164 0.114  0.0925
3 0.643  0.495 0.0172 0.0172
4 0.659  0.708 0.706  0.659 
5 0.692  0.812 0.0128 0.0128
6 0.166  0.417 0.889  0.166 
# A tibble: 6 x 4
       x     y      z      m
   <dbl> <dbl>  <dbl>  <dbl>
1 0.662  0.423 0.793  0.423 
2 0.0925 0.164 0.114  0.0925
3 0.643  0.495 0.0172 0.0172
4 0.659  0.708 0.706  0.659 
5 0.692  0.812 0.0128 0.0128
6 0.166  0.417 0.889  0.166 
`summarise()` regrouping output by 'sim' (override with `.groups` argument)
# A tibble: 6 x 2
# Groups:   sim [3]
    sim       z
  <dbl>   <dbl>
1     1  2.18  
2     2  9.49  
3     2  0.472 
4     3 -2.25  
5     3 -2.47  
6     3  0.0341
`summarise()` regrouping output by 'sim' (override with `.groups` argument)
# A tibble: 3 x 2
# Groups:   sim [3]
    sim z        
  <dbl> <list>   
1     1 <dbl [1]>
2     2 <dbl [2]>
3     3 <dbl [3]>

dplyr documentation built on June 19, 2021, 1:07 a.m.