R Basic

What is is.character() function in R

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!

Recent Posts

What is is.factor() Function in R

R consists of various data types, and "factor" is one of them. You can use…

19 hours ago

Efficiently Check If a Vector is Empty in R

The most efficient and idiomatic way to check if a vector is empty in R…

2 days ago

Checking If a Data Frame is Empty in R

What criteria are being evaluated to determine if a data frame is empty? There is…

3 days ago

How to Check If a List is Empty in R

What do we mean when we say an empty list? An empty list does not…

4 days ago

is.element() Function: Check Presence of Elements in R

Whether you want to do membership testing, filter data, identify missing values, check for duplicates,…

5 days ago

as.double() and is.double() Functions in R Language

Whether you want to perform calculations efficiently or derive accurate analysis, you need a double-precision…

1 week ago