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.
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.
is.character(input_object)
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”. |
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.
num_obj <- c(1921, 1918, 1906)
is.character(num_obj) # FALSE
As expected, it returns FALSE.
The logical vector contains only two values: TRUE and FALSE.
logical_obj <- c(FALSE, TRUE)
is.character(logical_obj) # FALSE
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.
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
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.
R consists of various data types, and "factor" is one of them. You can use…
The most efficient and idiomatic way to check if a vector is empty in R…
What criteria are being evaluated to determine if a data frame is empty? There is…
What do we mean when we say an empty list? An empty list does not…
Whether you want to do membership testing, filter data, identify missing values, check for duplicates,…
Whether you want to perform calculations efficiently or derive accurate analysis, you need a double-precision…