How to Use length() Function in R (4 Examples)


You can use the length() function in R to calculate the length of vectors, lists, and other objects.

This function uses the following basic syntax:

length(x)

where:

  • x: The name of the object to calculate length for

The following examples show how to use this function in different scenarios.

Example 1: Use length() with Vector

The following code shows how to use the length() function to calculate the number of elements in a vector:

#create vector
my_vector <- c(2, 7, 6, 6, 9, 10, 14, 13, 4, 20, NA)

#calculate length of vector
length(my_vector)

[1] 11

We can see that the vector has 11 total elements.

Note that length() also counts NA values.

To exclude NA values when calculating the length of a vector, we can use the following syntax:

#create vector
my_vector <- c(2, 7, 6, 6, 9, 10, 14, 13, 4, 20, NA)

#calculate length of vector, excluding NA values
sum(!is.na(my_vector))

[1] 10

We can see that the vector has 10 elements that are non-NA values.

Example 2: Use length() with List

The following code shows how to use the length() function to calculate the length of an entire list along with the length of a specific element in a list:

#create list
my_list <- list(A=1:5, B=c('hey', 'hi'), C=c(3, 5, 7))

#calculate length of entire list
length(my_list)

[1] 3

#calculate length of first element in list
length(my_list[[1]])

[1] 5

From the output we can see that the list has 3 total elements and we can see that the first element in the list has a length of 5.

Example 3: Use length() with Data Frame

If we use the length() function with a data frame in R, it will return the number of columns in the data frame:

#create data frame
df <- data.frame(team=c('A', 'B', 'B', 'B', 'C', 'D'),
                 points=c(10, 15, 29, 24, 30, 31))

#view data frame
df

  team points
1    A     10
2    B     15
3    B     29
4    B     24
5    C     30
6    D     31

#calculate length of data frame (returns number of columns)
length(df)

[1] 2 

If we would like to calculate the number of rows instead, we can use the nrow() function:

#calculate number of rows in data frame
nrow(df)

[1] 6

This tells us that there are 6 total rows in the data frame.

Example 4: Use length() with String

If we use the length() function with a string in R, it will typically just return a value of one:

#define string
my_string <- "hey there"

#calculate length of string
length(my_string)

[1] 1

To actually count the number of characters in a string, we can use the nchar() function instead:

#define string
my_string <- "hey there"

#calculate total characters in string
nchar(my_string)

[1] 9

This tells us that there are 9 total characters in the string, including spaces.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Count Observations by Group in R
How to Count Number of Rows in R
How to Select Random Rows in R

Leave a Reply

Your email address will not be published.