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().
rowwise(data, ...)
| data | Input data frame. |
|---|---|
| ... | < NB: unlike |
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.
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.
nest_by() for a convenient way of creating rowwise data frames
with nested data.
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 x 4 #> # Rowwise: #> x y z m #> <dbl> <dbl> <dbl> <dbl> #> 1 0.108 0.0560 0.174 0.113 #> 2 0.966 0.539 0.473 0.659 #> 3 0.114 0.249 0.383 0.248 #> 4 0.847 0.531 0.494 0.624 #> 5 0.153 0.304 0.701 0.386 #> 6 0.281 0.761 0.295 0.446# use c_across() to more easily select many variables df %>% rowwise() %>% mutate(m = mean(c_across(x:z)))#> # A tibble: 6 x 4 #> # Rowwise: #> x y z m #> <dbl> <dbl> <dbl> <dbl> #> 1 0.108 0.0560 0.174 0.113 #> 2 0.966 0.539 0.473 0.659 #> 3 0.114 0.249 0.383 0.248 #> 4 0.847 0.531 0.494 0.624 #> 5 0.153 0.304 0.701 0.386 #> 6 0.281 0.761 0.295 0.446#> # A tibble: 6 x 4 #> # Rowwise: #> x y z m #> <dbl> <dbl> <dbl> <dbl> #> 1 0.108 0.0560 0.174 0.0560 #> 2 0.966 0.539 0.473 0.473 #> 3 0.114 0.249 0.383 0.114 #> 4 0.847 0.531 0.494 0.494 #> 5 0.153 0.304 0.701 0.153 #> 6 0.281 0.761 0.295 0.281#> # A tibble: 6 x 4 #> x y z m #> <dbl> <dbl> <dbl> <dbl> #> 1 0.108 0.0560 0.174 0.0560 #> 2 0.966 0.539 0.473 0.473 #> 3 0.114 0.249 0.383 0.114 #> 4 0.847 0.531 0.494 0.494 #> 5 0.153 0.304 0.701 0.153 #> 6 0.281 0.761 0.295 0.281# 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))#>#> # A tibble: 6 x 2 #> # Groups: sim [3] #> sim z #> <dbl> <dbl> #> 1 1 0.688 #> 2 2 1.11 #> 3 2 7.13 #> 4 3 -1.68 #> 5 3 -3.53 #> 6 3 -1.16# If you want one row per simulation, put the results in a list() params %>% rowwise(sim) %>% summarise(z = list(rnorm(n, mean, sd)))#>#> # A tibble: 3 x 2 #> # Groups: sim [3] #> sim z #> <dbl> <list> #> 1 1 <dbl [1]> #> 2 2 <dbl [2]> #> 3 3 <dbl [3]>