Thursday, April 13, 2023

R Tutorial Playlist - List

 Lists are arrays in R but unlike vectors, lists can hold other lists and vectors.

a <- list()

a <- append(a, 1)

a <- append(a, "a")

a <- append(a, FALSE)

In the first line, we've declared list a, and in next three lines, we have appended elements of different types to it. Now, if you want to call some element from this list, you will have to provide this kind of call a[[1]], so we are calling first element from list a. In case of single element with lists, we have to provide this double angle bracket symbols.

In next example, we are going to create a list that is going to contain two vectors:

a <- list(a= c(1, 2, 3), b= c("a", "b", "c"))

Now, if we want to call one of those two vectors, we are going to perform the call in this way a$a. So with first a, we are calling list a and then $ sign, and at the end, name of our vector that is contained within the list. If you want to call second element of b vector from list a, you are going to call it in this way a$b[2].

For video tutorial, check the link below



No comments:

Post a Comment