Access Index Names of List Using lapply Function in R - GeeksforGeeks

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. 
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 listlst <- list(ele1 = 1:5,                       ele2 = c(FALSE,TRUE),                ele3 = LETTERS[4:9]) # printing list contentsprint ("List contents : ")print (lst) # accessing elementsprint ("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" 
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 sequenceto: Ending element of the sequenceby: Difference between the elementslength.out: Maximum length of the vector
Example: 
R
# declaring and creating a listlst <- list(ele1 = 1:5,                       ele2 = c(FALSE,TRUE),                ele3 = LETTERS[4:9]) # printing list contentsprint ("List contents : ")print (lst) # accessing elementsprint ("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" 
 
nnr223442
Picked
R List-Programs
R-List
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Convert Matrix to Dataframe in R