R does not have a “string” data type like other languages, but it does have a “character” type.
Whether you want to identify character columns, text processing, or data validation, you must check whether the object is a character or not before operating on it. That’s where you need a function that does identification.
is.character()
The is.character() is a built-in R function that checks whether an input object is a character or string. It returns TRUE if it is a character type and FALSE if not.
If you are using conditional statements to check specific conditions, this function will help you immensely.
Syntax
is.character(input_object)
Parameters
Name | Value |
input_object | It is an input object that checks if it is of a character type. It can be anything from “character”, “list”, “data frame”, “array”, or “factor”. |
Examples with Different Data Types
Character vector
Let’s declare a character vector and pass it to the is.character().
obj <- c("Yogita", "Mansi", "Krunal")
is.character(obj) # TRUE
As expected, the function returns TRUE for the character vector.
Numeric vector
num_obj <- c(1921, 1918, 1906)
is.character(num_obj) # FALSE
As expected, it returns FALSE.
Logical vector
The logical vector contains only two values: TRUE and FALSE.
logical_obj <- c(FALSE, TRUE)
is.character(logical_obj) # FALSE
List
The list is technically a vector, but it contains values from different data types.
main_list <- list(
name = "Vidisha",
age = 2,
city = "Rajkot"
)
is.character(main_list) #FALSE
The list itself is not a character object, but if you check for an individual element, it can be a character.
main_list <- list(
name = "Vidisha",
age = 2,
city = "Rajkot"
)
is.character(main_list$name) # TRUE
is.character(main_list$city) # TRUE
In the above code, the main_list is not a character, but it contains two character vectors, “name” and “city”. If you directly check that, it will return TRUE.
Data Frame
The data frame consists of rows and columns.
df <- data.frame(
name = c("Krunal", "Yogita"),
age = c(31, 27)
)
is.character(df) # FALSE
As expected, the data frame itself is not a character type, but its columns can be of type character.
For example, in the above code, data frame df has a character column called “name”. Let’s check for that.
df <- data.frame(
name = c("Krunal", "Yogita"),
age = c(31, 27)
)
is.character(df$name) # TRUE
Empty character
If you want to check whether an empty character is a character vector, use this function.
empty_string = ""
is.character(empty_string) # TRUE
An empty string or character is treated as a character vector by R.
That’s all, folks!
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.