Articles - Tips & Tricks

R: Find the Length of Every Elements in a List

  |   45611  |  Post a comment  |  Tips & Tricks  |  R programming
In R programming language, to find the length of every elements in a list, the function lengths() can be used.

A simplified format is as follow:

Code R :
 
lengths(x)
 


This function loops over x and returns a compatible vector containing the length of each element in x.

For example:

Code R :
 
my.list <- list(name = c("A", "B", "C"), age = c(20, 30, 40, 50))
lengths(my.list)
 


Output:

name  age 
   3    4