toColClasses: Change the Column Classes of Variables in a 'data.frame'

Description Usage Arguments Value Note Author(s) References Examples

View source: R/toColClasses.R

Description

Change the column classes of variables in a data.frame that has already been read into your workspace.

Usage

1
toColClasses(inDF, colClasses)

Arguments

inDF

The source data.frame.

colClasses

A character vector of the desired column classes. This should be the same length as the number of columns in the data.frame. If no change is desired, use "".

Value

A data.frame.

Note

This function has only been tested with a very small set of the as.* functions.

Author(s)

Ananda Mahto

References

http://stackoverflow.com/a/18893672/1270695

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
mydf <- data.frame(
  a = c(" 1"," 2", " 3"),
  b = c("a","b","c"),
  c = c(" 1.0", "NA", " 2.0"),
  d = c(" 1", "B", "2"),
  e = c(1, 0, 1))

mydf
str(mydf)

x <- toColClasses(mydf, c("as.integer", "", "as.numeric",
                          "as.factor", "as.logical"))
x
str(x)

y <- toColClasses(mydf, c("as.integer", "", "as.numeric",
                          "as.character", "as.logical"))
y
str(y)

mrdwab/SOfun documentation built on June 20, 2020, 6:15 p.m.