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

R length(): Vector, List, Matrix, Array, Data Frame, String

Before executing an operation on an object, it is advisable to check its length, as…

15 hours ago

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…

2 days ago

Adding Single or Multiple Columns to Data Frame in R

Whether you want to add new data to your existing datasets or create new variables…

4 days ago

sqrt() Function: Calculate Square Root in R

The square root of a number is a value that is multiplied by itself, giving…

5 days ago

How to Remove Duplicate Rows from DataFrame in R

Duplicate rows refer to all the values across all columns that are the same in…

6 days ago

How to Remove NA From Vector in R

A vector is a data structure that holds the same type of data. When working…

1 week ago