set.col.type: Set the Class of Columns in a Data Frame

Description Usage Arguments Details Value Author(s) See Also Examples

Description

This function converts the classes of columns to character, numeric, logical, integer or factor.

Usage

1
set.col.type(dat, col.type = list("character" = NULL), verbose = FALSE, ...)

Arguments

dat

A data frame

col.type

A named list of column names that are to be converted. The names of the list indicate the class to which the respective column should be converted (character, numeric, numeric.if.possible, logical, integer or factor)

verbose

if TRUE details about converted columns are printed on the console

...

Additional arguments to be passed to asNumericIfPossible

Details

Use col.type="numeric.if.possible" if conversion to numeric should be tested upfront, see asNumericIfPossible for details.

Value

A data frame with column classes changed according to the specifications in col.type

Author(s)

Martin Hecht, Karoline Sachse

See Also

asNumericIfPossible

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
str(d <- data.frame("var1" = 1, "var2" = TRUE, "var3" = FALSE,
    "var4" = as.factor(1), "var5" = as.factor("a"),"var6" = "b",
    stringsAsFactors = FALSE))

str(set.col.type(d))
str(set.col.type(d, list("numeric" = NULL)))
str(set.col.type(d, list("character" = c("var1" , "var2"),
    "numeric" = "var3", "logical" = "var4")))
str(set.col.type(d, list("numeric.if.possible" = NULL)))
str(set.col.type(d, list("numeric.if.possible" = NULL),
    transform.factors = TRUE))
str(set.col.type(d, list("numeric.if.possible" = NULL), transform.factors = TRUE,
    maintain.factor.scores = FALSE))

Example output

'data.frame':	1 obs. of  6 variables:
 $ var1: num 1
 $ var2: logi TRUE
 $ var3: logi FALSE
 $ var4: Factor w/ 1 level "1": 1
 $ var5: Factor w/ 1 level "a": 1
 $ var6: chr "b"
'data.frame':	1 obs. of  6 variables:
 $ var1: chr "1"
 $ var2: chr "TRUE"
 $ var3: chr "FALSE"
 $ var4: chr "1"
 $ var5: chr "a"
 $ var6: chr "b"
'data.frame':	1 obs. of  6 variables:
 $ var1: num 1
 $ var2: num 1
 $ var3: num 0
 $ var4: num 1
 $ var5: num 1
 $ var6: num NA
Warning message:
In doTryCatch(return(expr), name, parentenv, handler) :
  NAs introduced by coercion
'data.frame':	1 obs. of  6 variables:
 $ var1: chr "1"
 $ var2: chr "TRUE"
 $ var3: num 0
 $ var4: logi NA
 $ var5: Factor w/ 1 level "a": 1
 $ var6: chr "b"
'data.frame':	1 obs. of  6 variables:
 $ var1: num 1
 $ var2: num 1
 $ var3: num 0
 $ var4: num 1
 $ var5: Factor w/ 1 level "a": 1
 $ var6: chr "b"
Warning messages:
1: Variable can not be transformed to numeric. Use force.string = TRUE to force this. 
2: Variable can not be transformed to numeric. Use force.string = TRUE to force this. 
3: In set.col.type(d, list(numeric.if.possible = NULL)) :
  Conversion of variable(s) var5, var6 to numeric.if.possible failed.
'data.frame':	1 obs. of  6 variables:
 $ var1: num 1
 $ var2: num 1
 $ var3: num 0
 $ var4: num 1
 $ var5: Factor w/ 1 level "a": 1
 $ var6: chr "b"
Warning messages:
1: Variable can not be transformed to numeric. Use force.string = TRUE to force this. 
2: Variable can not be transformed to numeric. Use force.string = TRUE to force this. 
3: In set.col.type(d, list(numeric.if.possible = NULL), transform.factors = TRUE) :
  Conversion of variable(s) var5, var6 to numeric.if.possible failed.
'data.frame':	1 obs. of  6 variables:
 $ var1: num 1
 $ var2: num 1
 $ var3: num 0
 $ var4: num 1
 $ var5: num 1
 $ var6: chr "b"
Warning messages:
1: Variable can not be transformed to numeric. Use force.string = TRUE to force this. 
2: In set.col.type(d, list(numeric.if.possible = NULL), transform.factors = TRUE,  :
  Conversion of variable(s) var6 to numeric.if.possible failed.

eatTools documentation built on Nov. 10, 2021, 5:07 p.m.