Compute on a data frame a row-at-a-time. This is most useful when a vectorised function doesn't exist. Only mutate and summarise are supported so far.

rowwise_mutate(.data, ...)

rowwise_summarise(.data, ...)

Arguments

.data

A data.table

...

Name-value pairs of expressions

Value

A data.table

See also

Examples

# without rowwise
df <- data.table(x = 1:2, y = 3:4, z = 4:5)
df %>% mutate(m = mean(c(x, y, z)))
#>        x     y     z        m
#>    <int> <int> <int>    <num>
#> 1:     1     3     4 3.166667
#> 2:     2     4     5 3.166667
# with rowwise
df <- data.table(x = 1:2, y = 3:4, z = 4:5)
df %>% rowwise_mutate(m = mean(c(x, y, z)))
#>        x     y     z        m
#>    <int> <int> <int>    <num>
#> 1:     1     3     4 2.666667
#> 2:     2     4     5 3.666667


# # rowwise is also useful when doing simulations
params = fread(" sim n mean sd
  1  1     1   1
  2  2     2   4
  3  3    -1   2")

params %>%
  rowwise_summarise(sim,z = rnorm(n,mean,sd))
#>      sim          z
#>    <int>      <num>
#> 1:     1  1.9353632
#> 2:     2  2.7059544
#> 3:     2  2.9747419
#> 4:     3  2.2470978
#> 5:     3 -0.7759238
#> 6:     3 -1.2679940