List data structure use case

Hi Team,

Can someone please help me with some use case of List data structure. I know that its a collection of elements with different types. My confusion started with the below example. I know its a basic questions but still want to have clarity on this.

l1 <- list(FALSE, 1,  "two");
d1 <- data.frame(l1);

image
The same can be acheived using the below piece of code.

v1 <- c(FALSE);
v2 <- c(1);
v3 <- c("two");
d2 <- data.frame(v1, v2, v3);

The above piece of code produce the below output.
image

Do we have any specific use case, that can be achieved only with List but not with vector ?
Thanks for your time.

I think you've shown only that data.frames, becaues each column can support a different type, has similar flexibility to list, with respect to grouping things together of different types; Similar can be said for environments.

lists do well in representing non tabular heirarchies.

my_list_of_people_i_know <- list(friends = c("John","Mary"),enemeies= c("Max"))

also you can put fairly exotic stuff in lists without issue, like even functions.

Try the following code (which runs a simple linear regression using a built-in data set).

> m <- lm(wt ~ cyl, mtcars)
> str(m)

The fitted regression model is stored in variable m. The str() command displays the structure of m, which is a list with 12 components. The disparate nature of those components (numeric vectors, character vectors, scalars, a formula ...) makes a list the natural way to store it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.