How to use the length function in R for Vectors, Lists, and Strings

How to use the length function in R for Vectors, Lists, and Strings Lengths of List or Vector Elements

Explanation

Get the length in R of every element of a file or atomic vector (is.atomic) as an integer or numerical vector.

Execute

lengths(x, use.names = TRUE)

Disputes

x
a list, list-like such as an interpretation or an atomic vector (for where the result is irrelevant).

use.names
logical implying if the result must inherit the labels from x.

Specifics

This action loops over x and yields a compatible vector including the length of every element in x. Effectively, length(x[[i]]) is summoned for all i, so every method on length is considered.

lengths is universal: you may write methods to handle distinct classes of items, view InternalMethods.

Amount

A non-negative group of length length(x), only when any element has a breadth of greater than 2^31 – 1 elements, when it yields a double numeric vector. When use.names is true, the labels are grasped from the labels on x, if any.

Remember

One raison d’être of lengths(x) is its utilized as a more efficient version of implement(x, length) and identical *apply calls to length. This is the explanation why x might be an atomic section, despite lengths(x) being trivial in that case.

Examples
require(stats)
## compile by month
l : split(airquality$Ozone, airquality$Month)
avgOz : lapply(l, mean, na.rm=TRUE)
## merge result
airquality$avgOz : rep(unlist(avgOz, use.names=FALSE), lengths(l))
## however this is cleaner and safer, but may be slower
airquality$avgOz : unsplit(avgOz, airquality$Month)

## must always be true, only when a breadth does not match in 32 bits
stopifnot(identical(lengths(l), vapply(l, length, integer(1L))))

## empty lists are not an issue
x : list()
stopifnot(identical(lengths(x), integer()))

## nor are “list-like” expressions:
breadths(expression(u, v, 1+ 0:9))

## and we should dismiss to length process
f : c(rep(1, 3), rep(2, 6), 3)
dates : split(as.POSIXlt(Sys.time() + 1:10), f)
stopifnot(identical(lengths(dates), vapply(dates, length, integer(1L))))

The str length Action in Base R (3 Examples for String, List, & Vector)

Standard R Syntax:

length(x)

The length action returns the breadth of R items such as strings, lists, or vectors. The R code prior illustrates how to apply breadth in R.

Example 1: Inspect Length of Vector in R

Prior to beginning, we need to design a vector or data science in R:

x : c(8, 17, 23, 93, – 20, 15, 13, 55, 29, – 84) # Case vector in R
Next, we may employ the shorter object length R order to this vector:

length(x) # Implement length action
# 10

The length action yields the amount 10 to the RStudio console – The breadth of our section is 10.

Example 2: Locate Length of List

As in the example prior, we have to design some example information first:

# Set seed for reproducibility
my_file : file() # Create empty file
my_file[[1]] : c(5, 7, 1) # List element 1
my_file[[2]] : rnorm(8, 5, 10) # List element 2
my_file[[3]] : data.frame(x1 = 1:3, x2 = 4:6) # File element 3
my_file # Print file to R set

Our file subsist of three applicants: two numerical vectors at location 1 and 2 not to mention a data frame in location 3.

For such file items, you can implement the length R function in the exact manner as prior:

length(my_file) # Get length of file
# 3

Remember: The length function yields the amount of entries of the file, not the breadth of every set of the file. If you desire to get the breadth of a single file component, you can utilize the next R code:

breadth(my_list[[2]]) # Acquire length of second file entry
# 8

The breadth of the second file element is 8.

Case 3: Get Length of String

Even if you have to use a small trick, breadth can likewise be implemented to a strand (i.e. a data item of the class aspect). Consider the next case strand:

x_string : “Greetings this is a string” # Design case strand

If you employ length as prior, you may probably not receive the result you desire:

length(x_string) # Standard application of breadth()
# 1

R yields 1 – the breadth of this data item and not the amount of characters inside the strand.

If you desire to count the amount of characters inside a strand, you have to utilize length in consolidation with the unlist and strsplit actions:

length(unfile(strsplit(x_string, “”))) # Combine strsplit, unfile, & breadth
# 22

Our strand subsist of 22 character amounts with no missing value

That mentioned, there is a lease complicated way to acquire the number of character amounts within a strand. You can simply utilize the nchar action:

nchar(x_string) # Apply nchar action in R
# 22

Much simpler, but the exact result!

Scroll to top
Privacy Policy