R Basic

R type.convert() Function: Complete Guide

The type.convert() is a built-in R function that intelligently converts a vector, factor, or data frame column into the most appropriate data type (integer, numeric, logical, complex, or factor) based on its content.

Please note that we don’t need to specify which data type to convert to; this function automatically determines, which is the main beauty.

If you are working with an external dataset, you might come across data that has the incorrect data type, but for data analysis, you have to parse it to the appropriate data type, and that’s where this type of method is really helpful.

Syntax

type.convert(obj, ...)

Parameters

Name Value
obj An input object that will be converted to a vector, factor, or data frame.
na.strings (optional) Character vector specifying values to treat NA.(e.g., “NA”, “”)
as.is (logical) If TRUE, that means character vectors are not converted to factors.

If FALSE, character vectors may become factors.

dec (character) It is a decimal separator. By default, “.”. Use dec = “,” for European style.
numerals (string) Controls warning when converting numeric strings.

Return value

  1. If an input is a character vector, it returns a logical, numeric, or factor.
  2. If an input is a factor, it returns a numeric, character, or remains a factor.
  3. NA values will be introduced if na.strings is specified.

Basic conversion

Let’s define a vector with numeric values represented as a character vector.

Let’s try to convert that character vector into a numeric vector intelligently.

vec <- c("11", "21", "19")

print(class(vec)) 
# Output: [1] "character"

result <- type.convert(vec, as.is = TRUE)

print(result) 
# Output: [1] 11 21 19

print(class(result)) 
# Output: [1] "numeric"

In the above code, “11“, “21”, and “19” are valid numeric values wrapped in a character vector. That is why the type.convert() function easily converts them into a numeric vector.

If you don’t pass as.is argument, you will see a warning like this:

Warning message:

In type.convert.default(vec) :
  'as.is' should be specified by the caller; using TRUE

To fix this warning, you can pass as.is = TRUE argument to ensure that the character vector does not convert string values into factors, and we need numeric values.

Vector with mixed types

Using as.is = FALSE, you can convert a mixed-type vector to a factor. If you pass as.is = TRUE, a mixed-type vector does not convert to a factor.

mixed_vec <- c(11, "Eleven", TRUE)

print(class(mixed_vec)) 
# Output: [1] "character"

result <- type.convert(mixed_vec, as.is = FALSE)

print(result)
# Output: [1] 11 Eleven TRUE
# Levels: 11 Eleven TRUE

print(class(result)) 
# Output: [1] "factor"

The above code shows the output in a factor.

What if you pass as.is = TRUE, let’s find out.

mixed_vec <- c(11, "Eleven", TRUE)

print(class(mixed_vec)) 
# Output: [1] "character"

result <- type.convert(mixed_vec, as.is = TRUE)

print(result)
# Output: [1] 11 Eleven TRUE

print(class(result)) 
# Output: [1] "character"

The above conversion happened from character to character.

Handling NA values

To handle NA values, you must pass the “na.strings = NA” argument while calling the function.

vec_na <- c(11, NA, NA)

print(class(vec_na))
# Output: [1] "numeric"

result <- type.convert(vec_na, na.strings = "NA", as.is = TRUE)

print(result)
# Output: [1] 11 NA NA

print(class(result))
# Output: [1] "integer"

Using the “dec” argument

If you pass dec = “,”, it tells R that commas should be interpreted as decimal places.

vec_dec <- c("1,1", "1,9", "2,1")

print(class(vec_dec))
# Output: [1] "character"

result <- type.convert(vec_dec, as.is = TRUE, dec = ",")

print(result)
# Output: [1] 1.1 1.9 2.1

print(class(result))
# Output: [1] "numeric"

Factor to integer conversion

What if your input is a factor representing valid integer values? Well, type.convert() function returns a numeric vector containing integer values.

fac <- factor(c("10", "20", "30"))

print(class(fac))
# Output: [1] "factor"

result <- type.convert(fac, as.is = FALSE)

print(result)
# Output: [1] 10 20 30

print(class(result))
# Output: [1] "integer"

Logical conversion

If the input vector is a character vector representing logical vectors (TRUE or FALSE), the function will return the logical vector.

fac <- factor(c("TRUE", "FALSE", "TRUE"))

print(class(fac))
# Output: [1] "factor"

result <- type.convert(fac, as.is = FALSE)

print(result)
# Output: [1] TRUE FALSE TRUE

print(class(result))
# Output: [1] "logical"

That’s all!

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

1 week ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

1 week ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

2 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

3 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

1 month ago