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.
type.convert(obj, ...)
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. |
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.
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.
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"
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"
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"
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!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Whether you are reading or writing files via programs in the file system, it is…
When it comes to checking the data type of a variable, it depends on what…
The grepl() function (stands for "grep logical") in R searches for patterns within each element…
The zip() function creates a new zip archive file. You must ensure that the zip tool…
When working with file systems, checking the directory or file existence is always better before…
To create a grouped boxplot in R, we can use the ggplot2 library's aes() and…