Learn R Programming

tidyfst (version 0.7.7)

all_dt: Wise mutate or summarise with "all-at-if"

Description

In dplyr, there are functions like mutate_at, mutate_if and mutate_all, as well as summarise_at, summarise_if and summarise_all. Here we provide a simple way to do both in all_dt,at_dt and if_dt.

Usage

all_dt(data, .func, .funcs, ...)

at_dt(data, .at, .func, .funcs, ...)

if_dt(data, .if, .func, .funcs, ...)

Arguments

data

A data.frame

.func

Main function name,either "mutate(_dt)" or "summarise(_dt)"

.funcs

Function to be run within each column, should return a value or vectors with same length.

...

Parameters to be passed to parameter *.funcs*.

.at

Variables to select, could use regular expression or numeric/character vector.

.if

Conditional function to select columns. When starts with `-`(minus symbol), return the negative columns.

Value

A data.table

Details

Always return the columns with their original names after mutate or summarise.

Examples

Run this code
# NOT RUN {
# all_
all_dt(iris[,-5],mutate_dt,scale)
all_dt(iris[,-5],mutate,scale)
all_dt(iris[,-5],summarise,max)
all_dt(iris[,-5],summarise,min)

# at_
iris %>% at_dt("Se",mutate,scale)
iris %>% at_dt(1:3,mutate,scale)
iris %>% at_dt(c("Petal.Length"),mutate,scale,center = FALSE)

 # if_
 iris %>%
   if_dt(is.double,mutate_dt,scale,center = FALSE)
  ## support minus symbol to select negative conditions
 iris %>%
   if_dt(-is.factor,mutate_dt,scale,center = FALSE)
 iris %>%
   if_dt(is.factor,mutate,as.character)
 iris %>%
   if_dt(is.numeric,summarise,max,na.rm = TRUE)
# }

Run the code above in your browser using DataLab