1

I have the following data frame:

df <- tibble(x = runif(6), y = runif(6), z = runif(6))

And for the operation, I'd like to do it has to use dplyr::rowwise().

library(dplyr)
df <- tibble(x = runif(6), y = runif(6), z = runif(6))
df %>% 
  rowwise() %>%
  mutate(m = mean(c(x, y, z)))
#> # A tibble: 6 × 4
#> # Rowwise: 
#>       x     y      z     m
#>   <dbl> <dbl>  <dbl> <dbl>
#> 1 0.606 0.452 0.799  0.619
#> 2 0.760 0.168 0.666  0.531
#> 3 0.125 0.792 0.105  0.341
#> 4 0.431 0.714 0.178  0.441
#> 5 0.430 0.115 0.676  0.407
#> 6 0.290 0.830 0.0335 0.385

What I'd like to do next is to use dtplyr to speed up the process. But I found an error:

library(dtplyr)
library(dplyr)
df <- tibble(x = runif(6), y = runif(6), z = runif(6))
df.dt <- lazy_dt(df)
df.dt %>%
  rowwise() %>%
  mutate(m = mean(c(x, y, z)))
#> Error in UseMethod("rowwise"): no applicable method for 'rowwise' applied to an object of class "c('dtplyr_step_first', 'dtplyr_step')"

What's the right way to do it?

littleworth
  • 4,781
  • 6
  • 42
  • 76
  • 1
    rowwise is not implemented yet, see https://github.com/tidyverse/dtplyr/issues/236 – zx8754 Nov 15 '22 at 08:17
  • 4
    For simple use cases you could emulate `rowwise()` by grouping by `row_number()`, e.g. `df.dt %>% group_by(row_number()) %>% mutate(m = mean(c(x, y, z)))`. – Ritchie Sacramento Nov 15 '22 at 08:30

0 Answers0