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 scale(): Scaling and Centering of Matrix-like Objects

The scale() function in R centers (subtracting the mean) and/or scales (dividing by the standard…

2 weeks ago

file.rename(): Renaming Single and Multiple Files in R

To rename a file in R, you can use the file.rename() function. It renames a…

3 weeks ago

R prop.table() Function

The prop.table() function in R calculates the proportion or relative frequency of values in a…

3 weeks ago

exp() Function: Calculate Exponential of a Number in R

The exp() is a built-in function that calculates the exponential of its input, raising Euler's…

3 weeks ago

R split() Function: Splitting a Data

The split() function divides the input data into groups based on some criteria, typically specified…

1 month ago

colMeans(): Calculating the Mean of Columns in R Data Frame

The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

1 month ago