convert_cols: Convert Multiple Columns of a 'data.frame' All at once...

View source: R/modeling.R

convert_colsR Documentation

Convert Multiple Columns of a data.frame All at once conversion of a data.frame from current column types to alternates.

Description

Convert Multiple Columns of a data.frame

All at once conversion of a data.frame from current column types to alternates.

Usage

convert_cols(d, cast)

Arguments

d

A data.frame that needs to have specific columns converted.

cast

A ⁠string vector⁠ containing either: "n" (numeric), "c" (character), or "f" (factor).

Value

A data.frame with converted column types.

Examples


n = 100

st = sample(LETTERS, n, replace = TRUE)
sr = sample(letters, n, replace = TRUE)
num = rnorm(n)

d = data.frame(x = st, y = num, z = sr, stringsAsFactors = FALSE)

# Convert all columns

o = convert_cols(d,c("f", "c", "f"))

# Convert a subset
d[, c(1, 3)] = convert_cols(d[, c(1, 3)], c("f", "f"))

coatless/balamuta documentation built on Nov. 16, 2023, 5:30 a.m.