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

R View() Function

The View() is a utility function in R that invokes a more intuitive spreadsheet-style data…

6 days ago

summary() Function: Producing Summary Statistics in R

The summary() is a generic function that produces the summary statistics for various R objects,…

2 weeks ago

R paste() Function

The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…

3 weeks ago

paste0() Function in R

R paste0() function concatenates strings without any separator between them. It is a shorthand version…

3 weeks ago

How to Calculate Standard Error in R

Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…

3 weeks ago

R max() and min() Functions

max() The max() function in R finds the maximum value of a vector or data…

4 weeks ago