Lists in R

Different types of data structures in R includes vectors, matrices, data frames and lists. Having already discussed vectors, matrices and data frames, the focus of this article is lists. The lists are type of data structures that allow you to store and organize different type of data types including vectors, matrices and even other lists.

Unlike vectors, lists are not limited to containing only one kind of data type i.e. numeric or character data at a time, they can hold diverse data types. To start working with lists, let’s consider an example involving a football game. Let’s say we have a data that contains details of a game i.e. list of of players, name of the game and another vector that contains the number of times game is won by players.

Now if we have to work with vectors, we have to have separate vectors for each piece of information as shown below.

Download Example File
game <- "Football"
players <- data.frame(f_name =  c("Stephen","Chris","Derrick"),
                                   l_name =  c("Cury","Paul","Rose"),
                                   age    =  c(32 , 29 , 34))
 wins <- c(10,15,11) 

However, in lists we can combine each of the above vectors in a single list, that would contain detail of the game, players information and the games won by each player. The command for creating list would be as follows

g_list <- list(game,players,wins)

To access the detail of above command, g_list is used, which gives the following output

Accessing Data from Lists

When it comes to accessing data from lists in R, there are three methods to do so. The first is using a single square bracket [] , the second is by using double square bracket [[]] and the third is by using the dollar sign $.

We can understand it further by using the list we created above. In the g_list, if we wish to access third component of list, we can use the following command.

g_list[[3]]

Remember that third component of the list is a number of wins by each player, so we get the following result from above command

Similarly, if we want to access specific elements within a vector or data frame component of the list, a single or double bracket can be used. For instance, to extract the value 15 from the third component , we use the following command

g_list[[3]][2]

Naming components of lists in R

Instead of using numerical indices, we can also assign names to the lists, which makes it easier to work with lists. If we want to access the names of the previous lists created, we use the following command

names(g_list)

However, the output we get tells us that there are no names assigned to the lists.

To assign names to lists, we use the names() function in R. For instance, if we want to name list using the components as “game_type,” “player_list,” and “player_wins,” the command would be

names(g_list) <- c("Game_Type","Players","Games_won")

The command would assign names to lists sequence wise.

The names of components can also be deleted from lists, by using following command

names(g_list) <- NULL

Access data using Names from lists in R

Once the components are named, accessing specific data becomes straightforward using the dollar sign notation. For instance, to access the component named “games_won,” one simply employs the following command

g_list$Games_won

This command would give the detail of number of games won by each player.

Now, to access a certain component within the list games_won, we use the following command. The command will give us the detail of second component within the list.

g_list$Games_won[2]

The output for above commands is

Adding or Removing components from lists

In R, you can add or remove components from a list. To add a 4th component in the list, use the following command with text in inverted commas as the 4th component.

g_list[4] <- "This is 4th component"

The component will be added in the lists.

Similarly, to remove a component from the lists in R, the index or name of the component should be used in the following way

g_list[[1]] <- NULL 

By removing the component, the indexing of whole list will change. Now, if you access a 2nd component through the index from the list, the output will be the third component. This is because first component was removed. That is why using names instead of index number is a great idea.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Tweet
Share
Share
Pin