apply_row: Apply a function rowwise, selecting variables with...

View source: R/apply_row.R

apply_rowR Documentation

Apply a function rowwise, selecting variables with dplyr::select() syntax

Description

apply_row() is a wrapper around apply() and select(), applying a function rowwise, and selecting variables with dplyr::select() syntax. This makes code slightly less verbose for rowwise operations.

Usage

apply_row(x, select_helpers = everything(), FUN, ...)

Arguments

x

Data frame or tibble to pass through.

select_helpers

Select variables using dplyr::select() syntax

FUN

Function to be applied to selected columns

...

Additional arguments to the function.

Examples

## Not run: 
library(tidyverse)
iris %>% mutate(Sepal_Sum = apply_row(.,select_helpers = contains("Sepal"), sum, na.rm = TRUE))
iris %>% mutate(Sepal_Sum = apply_row(.,c("Sepal.Length","Petal.Width"), function(x) sum(x > 1))) 

Can use a character vector to select columns
petal_str <- c("Petal.Length", "Petal.Width")
iris %>% mutate(Any_Petal = apply_row(., petal_str, function(x) any(x > 1)))

## End(Not run)



martinctc/surveytools documentation built on Nov. 21, 2024, 10:48 p.m.