lapply_rows: Apply a function over rows of a 'data.frame'-like object and...

lapply_rowsR Documentation

Apply a function over rows of a data.frame-like object and concatenate the results back to tibble or data.frame.

Description

Inside a FUN, a row of df will be available as a list or scdrake_list() (default). The FUN must return a named list for a proper concatenation of the results. The named list can also contain elements of a length other than one, which are then wrapped in list().

Usage

lapply_rows(df, as_scdrake_list = TRUE, return_tibble = TRUE, FUN, ...)

Arguments

df

A data.frame-like object.

as_scdrake_list

A logical scalar: if TRUE, each row-list in FUN will be converted to scdrake_list().

return_tibble

If TRUE, a tibble with concatenated results is returned. Otherwise results are coerced to data.frame and original rownames are set.

FUN

A function to apply over df rows.

...

Additional arguments passed to FUN.

Value

tibble or data.frame according to the return_tibble parameter.

Examples

library(magrittr)

fn <- function(row_list) {
  row_list$cyl_2 <- row_list$cyl**2
  row_list$colors <- c("red", "green", "blue")
  row_list$sublist <- mtcars[1:5, 1:5]
  return(row_list)
}

df <- lapply_rows(mtcars, FUN = fn)
head(df)

df2 <- lapply_rows(mtcars, return_tibble = FALSE, FUN = fn)
head(df2)

bioinfocz/scdrake documentation built on Sept. 19, 2024, 4:43 p.m.