Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if the data is a list or data.frame in R

Tags:

How do I know if my data in R is a list or a data.frame?

If I use typeof(x) it says list, if I use class(x) it says data.frame?

like image 462
carlosmaria Avatar asked Jul 23 '16 08:07

carlosmaria


People also ask

What is the difference between data frame and list in R?

DataFrames are generic data objects of R which are used to store the tabular data. They are two-dimensional, heterogeneous data structures. A list in R, however, comprises of elements, vectors, data frames, variables, or lists that may belong to different data types.

How do I tell if something is a Dataframe in R?

frame() function in R Language is used to return TRUE if the specified data type is a data frame else return FALSE.

Is a data frame a list in R?

A Data frame is simply a List of a specified class called “data. frame”, but the components of the list must be vectors (numeric, character, logical), factors, matrices (numeric), lists, or even other data frames.

How do you check if something is a list in R?

list() Function. is. list() function in R Language is used to return TRUE if the specified data is in the form of list, else returns FALSE.


1 Answers

To clarify a possible misunderstanding given the title of your question, a data.frame is also a list.

is.list(data.frame())   # TRUE

However, you can use inherits() to see if an object is a list or data.frame

inherits(data.frame(), "data.frame")  # TRUE
inherits(list(), "data.frame")        # FALSE

inherits(data.frame(), "list")        # FALSE
inherits(list(), "list")              # TRUE
like image 107
SymbolixAU Avatar answered Sep 29 '22 12:09

SymbolixAU