R Basic

is.list() Function: Check If an Object is List in R

The is.list() function in R checks if an input object is a list. It returns TRUE if an object is a list and FALSE otherwise. This function checks if an object itself is a list, not the elements of the list. 

For example, if a list has vector and data frames, it is still a list. It does not matter the type of elements of the list.

Some functions require input to be a list, and we can use this function to ensure that what we are passing is a list. This function helps us confirm that we are, in fact, dealing with lists and ensures an error-free process.

Syntax

is.list(obj)

Parameters

Name Value
obj It is an input object that you want to check for a list.

Checking list

main_list <- list(1, "kb", 3.14, c(1, 2, 3))

is.list(main_list) # [1] TRUE

We created a list of different types, printed it, and check if it is a list object. As expected, the output is TRUE.

Checking list elements

If you check for specific elements of the list, that element must be a list to get the output TRUE; otherwise, it is FALSE.

main_list <- list(1, "kb", 3.14, c(1, 2, 3))

is.list(main_list[[4]]) # [1] FALSE

Since the fourth element of the list is a vector, we get the output FALSE.

Checking non-list type

vec <- c(11, 21)

is.list(vec) # [1] FALSE

As expected, if you pass anything other than the list, it returns FALSE.

Proper application

One proper application of this function is to use with if statement and create a condition where an object must be a list, and if it is not, then show something meaningful to the user.

main_list <- list(1, "kb", 3.14, c(1, 2, 3))

# Use if statement with is.list
if (is.list(main_list)) {
  print("main_list is a list")
} else {
  print("main_list is not a list")
}

# Output: [1] "main_list is a list"

is.list() vs typeof()

The main difference between is.list() and typeof() functions is that is.list() function checks if an object is of the class list, whereas the typeof() function checks the internal storage mode of the object.

main_list <- list(1, "kb", 3.14, c(1, 2, 3))

is.list(main_list) # [1] TRUE

typeof(main_list) # [1] "list"

That’s all!

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

2 weeks ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

2 weeks ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

3 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

4 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

2 months ago