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?
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.
frame() function in R Language is used to return TRUE if the specified data type is a data frame else return FALSE.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With