construct_cols: Subset dataframe columns

View source: R/private_funs.R

construct_colsR Documentation

Subset dataframe columns

Description

This function mostly exists so that I don't have to keep typing df[2:length(df)] or some other variant of it. It lets me subset columns based on contiguous or non-contiguous ranges.

Usage

construct_cols(df, from = 1, to = NULL, cols = NULL)

Arguments

df

(Dataframe) A dataframe.

from, to

(Numeric or NULL) The start and end of a continuous range of columns that will be considered for the empty/not-empty decision. For example, columns that are always filled should be omitted (see examples). If to is NULL, it defaults to the last column in df so that ⁠from = 2, to = NULL⁠ is the same as 2:length(df).

cols

(Numeric or NULL) A numeric vector of the columns to consider. This allows you to select non-contiguous columns. If the cols argument is being used (not-NULL), from and to will be ignored.

Value

A numeric vector that can be used to reference columns in df.

Examples

## Not run: 
cols <- construct_cols(mtcars, from = 7)
cols

#> [1]  7  8  9 10 11

mtcars[cols]

#>                      qsec vs am gear carb
#> Mazda RX4           16.46  0  1    4    4
#> Mazda RX4 Wag       17.02  0  1    4    4
#> Datsun 710          18.61  1  1    4    1
#> Hornet 4 Drive      19.44  1  0    3    1

## End(Not run)


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.