R Program to get the length of the first two vectors of a given list

How to get the length of the first two vectors of a given list

Here we are explaining how to write an R program to get the length of the first two vectors of a given list. Here we are using a built-in functions list(),length() for this. The function list() helps to create a list and length() for getting or set the length of vectors and factors, and of any other R object. The syntax of these functions are

list(…)#Where ....(dots)are the objects, possibly named

length(x)#Where x is an R object like vector or factor

How to get the length of the first two vectors of a given list using the R program

Below are the steps used in the R program to get the length of the first two vectors of a given list. In this R program, we directly give the values to a built-in function list(). Here we are using variables L1 for holding the list elements g1,g2,g3. Call the function list() with different types of elements. Use the function length() for finding the length of vectors in the list as like length(L1$g1), length(L1$g2). Finally, print the length.

ALGORITHM

STEP 1: Assign variables L1 with a list

STEP 2: Create L1 with 3 sets of elements g1,g2,g3

STEP 3: Print the original lists

STEP 4: Find the length of vectors in the list as like length(L1$g1), length(L1$g2)

STEP 5: Print the length of vectors

R Source Code

                                          L1 = list(g1 = 1:5, g2 = "C Programming", g3 = "JAVA")
print("Original list:")
print(L1)
print("Length of the vector 'g1' and 'g2' of the list")
print(length(L1$g1))
print(length(L1$g2))
                                      

OUTPUT

[1] "Original list:"
$g1
[1] 1 2 3 4 5

$g2
[1] "C Programming"

$g3
[1] "JAVA"

[1] "Length of the vector 'g1' and 'g2' of the list"
[1] 5
[1] 1