Warning: session_start(): open(/tmp/sess_0683458db9488df89644515c1ba6ae89, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
length() function in R: How to use the length function in R? - LearnShareIT

length() function in R: How to use the length function in R?

length() function in r

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:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *