Change the column classes of variables in a data.frame
that has
already been read into your workspace.
toColClasses(inDF, colClasses)
inDF | The source |
---|---|
colClasses | A character vector of the desired column classes. This
should be the same length as the number of columns in the |
A data.frame
.
This function has only been tested with a very small set of the
as.*
functions.
http://stackoverflow.com/a/18893672/1270695
Ananda Mahto
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 1str(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#> a b c d e #> 1 1 a 1 1 TRUE #> 2 2 b NA B FALSE #> 3 3 c 2 2 TRUEstr(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#> a b c d e #> 1 1 a 1 1 TRUE #> 2 2 b NA B FALSE #> 3 3 c 2 2 TRUEstr(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