In this article, we will show you how to use the **length() function in R.** The length() function helps you get or set the size of the R object. Let’s follow this article to learn more about it with the explanation and examples below.

**length() function in R**

**What does length() do?**

The** **length() function can help you get or set the length of objects, such as the vector, the array, the string, the matrix, or the data frame,…

**Syntax**:

Get the length of the R object.

`length(object)`

Set the length of the R object.

`length(object) <- value`

**Parameters**

**object:**The R object.**value:**The new length of the R object.

After learning the usage and syntax of the length() function, you will learn how to use it in the next title below.

**How to use the length(**)** function**?

The length() function can help you get the size of the R object.

**Get the length of the string**

You can not get the length of the string by the usual method. If you use it, the function always returns 1. Alternatively, you have to convert the string to a list before getting its length.

Look at the example below:

source = 'LearnShareIT' # Convert the string to a list before using the length() function length(unlist(strsplit(source,'')))

**Output**

`[1] 12`

**Get the size of the vector**

You can get the size of the vector by the length() function.

Look at the example below:

vector <- c(0,0,0,0,0,1,0,1) # Get the length of the vector length(vector)

**Output**

`[1] 8`

**Set the size of the vector**

You can get the size of the vector by the length() function.

Look at the example below:

vector <- c(0,0,0,0,0,1,0,1) # Set the length of the vector length(vector) <- 4 vector

**Output**

`[1] 0 0 0 0`

You can get or set the length of the array by the same method as the vector.

**Get the amount of columns in the data frame**

You can get the number of columns in the data frame by the length() function.

Look at the example below:

df <- data.frame( Animals =c('Cat', 'Dog', 'Lion', 'Tiger'), Color = c('Yellow', 'Black', 'Orange', 'Pink') ) # Get the number of columns length(df)

**Output**

`[1] 2`

You can flexibly and creatively get the size of the attributes in the data frame by the length() function.

**Summary**

You have learned about the **length() function in R. **By this function, you can get or set the length of the R object. And with a string, you have to convert it to a list before using the length() function. We hope this tutorial is helpful to you. Thanks!

**Maybe you are interested**:

- pt() function in R: How to use the pt function in R
- print() Function In R Programming Language
- optim() function in R: How to use optim function in R?

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!

**Name of the university:** PTIT

**Major**: IT

**Programming Languages**: C, C++, Java, Python, JavaScript, R