Multiple conversion

Share:

Description

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

Usage

1
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 fucntion

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 the columns in the data frame, but you can specify just a few of them. 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 asked.

Author(s)

A. Marcia Barbosa

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.