Length of R vector
In R, the length of a vector is determined by the number of elements it contains. we can use the length()
function to retrieve the length of a vector.
R
# Create a numeric vector x <- c (1, 2, 3, 4, 5) # Find the length of the vector length (x) # Create a character vector y <- c ( "apple" , "banana" , "cherry" ) # Find the length of the vector length (y) # Create a logical vector z <- c ( TRUE , FALSE , TRUE , TRUE ) # Find the length of the vector length (z) |
Output:
> length(x)
[1] 5
> length(y)
[1] 3
> length(z)
[1] 4
R Vectors
R Vectors are the same as the arrays in R language which are used to hold multiple data values of the same type. One major key point is that in R Programming Language the indexing of the vector will start from ‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well.