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

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.

References

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

Author

Ananda Mahto

Examples

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
#> a b c d e #> 1 1 a 1.0 1 1 #> 2 2 b NA B 0 #> 3 3 c 2.0 2 1
str(mydf)
#> 'data.frame': 3 obs. of 5 variables: #> $ a: Factor w/ 3 levels " 1"," 2"," 3": 1 2 3 #> $ b: Factor w/ 3 levels "a","b","c": 1 2 3 #> $ c: Factor w/ 3 levels " 1.0"," 2.0",..: 1 3 2 #> $ d: Factor w/ 3 levels " 1","2","B": 1 3 2 #> $ e: num 1 0 1
x <- toColClasses(mydf, c("as.integer", "", "as.numeric", "as.factor", "as.logical")) x
#> a b c d e #> 1 1 a 1 1 TRUE #> 2 2 b NA B FALSE #> 3 3 c 2 2 TRUE
str(x)
#> 'data.frame': 3 obs. of 5 variables: #> $ a: int 1 2 3 #> $ b: Factor w/ 3 levels "a","b","c": 1 2 3 #> $ c: num 1 NA 2 #> $ d: Factor w/ 3 levels " 1","2","B": 1 3 2 #> $ e: logi TRUE FALSE TRUE
y <- toColClasses(mydf, c("as.integer", "", "as.numeric", "as.character", "as.logical")) y
#> a b c d e #> 1 1 a 1 1 TRUE #> 2 2 b NA B FALSE #> 3 3 c 2 2 TRUE
str(y)
#> 'data.frame': 3 obs. of 5 variables: #> $ a: int 1 2 3 #> $ b: Factor w/ 3 levels "a","b","c": 1 2 3 #> $ c: num 1 NA 2 #> $ d: chr " 1" "B" "2" #> $ e: logi TRUE FALSE TRUE