Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Access Index Names of List Using lapply Function in R

  • Last Updated : 13 Dec, 2021

The lapply() method in R programming language returns a list of the same length as that of the supplied vector, each element of which is obtained as the result of applying FUN to the corresponding element of the vector.

Syntax:

lapply (X, FUN, )

Parameter : 

X – an atomic vector or list to apply the function on

FUN – the function to be applied over each element of X. 

Method 1: Using seq_along()

The seq_along() method in R is used to generate a sequence of elements of a length equivalent to that of the input length of the passed argument. The generated sequence always begins with the integer values starting from 1. 

Syntax:

seq_along (X)

Parameter : 

X – passed argument

The seq_along() generated sequence is passed as the first argument of the function lapply(). The function FUN comprises the concatenation of the names assigned to different components of the list which can be accessed using the index and the list name along with the component vectors or list values appended together using the comma separator. The index names of the specified list can be accessed using the following syntax : 

names (list-obj)[[index-cntr]]

The element values can be accessed using the following syntax : 

list-obj [[index-cntr]]

Example:

R




# declaring and creating a list
lst <- list(ele1 = 1:5,       
                ele2 = c(FALSE,TRUE),
                ele3 = LETTERS[4:9])
 
# printing list contents
print ("List contents : ")
print (lst)
 
# accessing elements
print ("Accessing elements : ")
lapply(seq_along(lst),
       function(i) paste(names(lst)[[i]],
                         "->",
                         paste(lst[[i]], collapse = ", ")))

Output 

[1] "List contents : " 
$ele1 
[1] 1 2 3 4 5  
$ele2 
[1] FALSE  TRUE  
$ele3 
[1] "D" "E" "F" "G" "H" "I"  
[1] "Accessing elements : "
[[1]] 
[1] "ele1 -> 1, 2, 3, 4, 5"  
[[2]] 
[1] "ele2 -> FALSE, TRUE"  
[[3]] 
[1] "ele3 -> D, E, F, G, H, I" 

Method 2: Using seq()

The seq() method can also be used to traverse through the list and its components individually. The method works similarly to the seq_along() method. 

seq() function in R is used to create a sequence of elements in a Vector. It takes the length and difference between values as an optional argument. 

Syntax:
seq(from, to, by, length.out)

Parameters:
from: Starting element of the sequence
to: Ending element of the sequence
by: Difference between the elements
length.out: Maximum length of the vector

Example: 

R




# declaring and creating a list
lst <- list(ele1 = 1:5,       
                ele2 = c(FALSE,TRUE),
                ele3 = LETTERS[4:9])
 
# printing list contents
print ("List contents : ")
print (lst)
 
# accessing elements
print ("Accessing elements : ")
lapply(seq(lst),
       function(i) paste(names(lst)[[i]], "->",
                         paste(lst[[i]], collapse = ", ")))

Output 

[1] "List contents : " 
$ele1 
[1] 1 2 3 4 5  
$ele2 
[1] FALSE  TRUE  
$ele3 
[1] "D" "E" "F" "G" "H" "I"  
[1] "Accessing elements : "
[[1]] 
[1] "ele1 -> 1, 2, 3, 4, 5"  
[[2]] 
[1] "ele2 -> FALSE, TRUE"  
[[3]] 
[1] "ele3 -> D, E, F, G, H, I" 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!