multConvert: Multiple conversion

View source: R/multConvert.R

multConvertR Documentation

Multiple conversion

Description

This function can simultaneously convert multiple columns of a matrix or data frame.

Usage

multConvert(data, conversion, cols = 1:ncol(data))

Arguments

data

A matrix or data frame containing columns that need to be converted

conversion

the conversion to apply, e.g. as.factor or a custom-made function

cols

the columns of 'data' to convert

Details

Sometimes we need to change the data type (class, mode) of a variable in R. There are various possible conversions, performed by functions like as.integer, as.factor or as.character. If we need to perform the same conversion on a number of variables (columns) in a data frame, we can convert them all simultaneously using this function. By default it converts all columns in 'data', but you can specify just some of those. 'multConvert' can also be used to apply other kinds of transformations – for example, if you need to divide some of your columns by 100, just write a function to do this and then use 'multConvert' to apply this function to any group of columns.

Value

The input data with the specified columns converted as specified in 'conversion'.

Author(s)

A. Marcia Barbosa

Examples

data(rotif.env)

str(rotif.env)

# convert the first 4 columns to character:
converted.rotif.env <- multConvert(data = rotif.env, 
conversion = as.character, cols = 1:4)

str(converted.rotif.env)


names(rotif.env)

# divide some columns by 100:

div100 <- function(x) x / 100

rotif.env.cent <- multConvert(data = rotif.env, 
conversion = div100, cols = c(6:10, 12:17))

head(rotif.env.cent)

fuzzySim documentation built on Oct. 9, 2023, 5:09 p.m.