Rowwise in dplyr 1.0 for data frame

I am happy that rowwise() is back on the table with dplyr 1.0. Works nice when I have a function that takes individual parameters, but how do I pass one row of a data frame to my function?

library(dplyr, warn.conflicts = FALSE) # 0.8.99.9002
# https://dplyr.tidyverse.org/dev/articles/rowwise.html
use_par = function(x, y, z){
  x+y+z  
}

# Output is what I want, but...
df = tibble(name = LETTERS[1:2], x = 1:2, y = 3:4, z = 5:6)
df %>% rowwise(name) %>% 
  summarize(s = use_par(x,y,z))
#> # A tibble: 2 x 2
#> # Rowwise:  name
#>   name      s
#>   <chr> <int>
#> 1 A         9
#> 2 B        12


# ... I have a function that takes one row of a data frame
# with dozens of columns I don't want to list individually
use_df = function(df){
  # assume one row of a data frame
  stopifnot(nrow(df) == 1)
  df$x + df$y + df$z
}

# Something like this?
df %>% rowwise(name) %>% 
  summarize(across(everything(), use_df))
#> Error: `summarise()` argument `..1` errored.
#> i `..1` is `across(everything(), use_df)`.
#> i The error occured in row 1.
#> x $ operator is invalid for atomic vectors

I apologise in advance for not directly adressing your question, but it struck me that this might be a good use case for a slide function from the slider package. I slightly modified use_df function so that it outputs results into a named list.

use_df = function(df){
  # assume one row of a data frame
  stopifnot(nrow(df) == 1)
  list(name=df$name,value=df$x + df$y + df$z)
}

slider::slide_dfr(df,use_df)
# name value
#   A     9
#   B    12

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Simple, and useful. I am using a loop over all rows for quick-and-dirty code, but wanted to give the new rowwise concept a try.

While I love the concepts of tidyverse, the eternal up and down for rowwise operation is one of the things I don't like, and I am surprised that the syntax has become uglier over time. do was not so bad. Working on long-data is the preferred style, but on the other hand there are tons of examples for column-wise operations, but rowwise often leads to "you should not do this" comments.

Thanks for your solution, and hoping someone will enlighten me how to use the tidyverse style.

Yes, I had read: