Built with Tutify.io

    AIStudyLabs

    R head() & tail() Functions

    R head() & tail() Functions

    What is the head() and tail() functions do in R? The head() in R is used to get the first rows of the DataFrame, Vector, or compatible object. The tail() is used to get the last rows of the DataFrame, Vector, or a compatible object.

    <a id="head-in-r"></a>

    1. R head() Function

    The head() function is used to get the first few elements from the DataFrame or the first few elements from the Vector. It takes two arguments the input object and the number of rows you wanted to get from the first.

    1.1 Syntax of head()

    The following is the syntax of the head() function.

    
    # Syntax
    head(x,n=number)
    

    1.2 head() Example in R

    In the following example, I have demonstrated getting the first few rows from the DataFrame. The first example doesn't take the number as input hence, it returns the default count. And the second example takes 3 as the number hence, it returns the first 3 rows from the DataFrame

    
    # head() example
    head(df)
    
    # head with number
    head(df,3)
    

    Yields below output.

    <a id="tail-in-r"></a>

    2. R tail() Function

    The R tail() function is used to get the last few elements from the DataFrame or the last few elements from the Vector. It takes two arguments the input object and the number of rows you wanted to get from the last.

    2.1 Syntax of tail()

    The following is the syntax of the tail() function.

    
    # Syntax
    tail(x,n=number)
    

    2.2 tail() Example in R

    In the following example, I have demonstrated getting the last few rows from the DataFrame. The first example doesn't take the number as input hence, it returns the default count. And the second example takes 3 as the number hence, it returns the last 3 rows from the DataFrame

    
    #tail
    tail(df)
    
    #tail with size
    tail(df,3)
    

    3. Vector head() & tail()

    In all the above examples, we have seen getting head and tail rows from the R DataFrame. These methods can also be used to get the first and last elements from the R Vector.

    Let's create the R character Vector.

    
    # Create vector
    vec <- c('Scott','Ram','Deepika','Sravan','Ann','Hans','Jeff','Harly')
    
    # head of the Vector
    head(vec)
    head(vec,4)
    
    # tail of the Vector
    tail(vec)
    tail(vec,4)
    

    Yields below output.

    4. R matrix head() & tail()

    Let's see how to get the head() and tail() of the matrix object in R.

    
    # Create R matrix
    data <- c(10,11,12,13,14,15,16,17,18)
    mtx <- matrix(data,nrow=3,ncol=3,byrow=TRUE)
    mtx
    
    #head() matrix
    head(mtx)
    head(mtx,4)
    
    #tail() matrix
    tail(mtx)
    tail(mtx,4)
    

    4. Complete Example

    Following is a complete example of using head() and tail() functions in R.

    
    # Create DataFrame
    df <- data.frame(
      id = c(10,11,12,13,18,20,25,40),
      name = c('Scott','Ram','Deepika','Sravan','Ann','Hans','Jeff','Harly'),
      dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))
    )
    
    #head
    head(df)
    
    #head with size
    head(df,3)
    
    #tail
    tail(df)
    
    #tail with size
    tail(df,3)
    
    # Create vector
    vec <- c('Scott','Ram','Deepika','Sravan','Ann','Hans','Jeff','Harly')
    
    # head of the Vector
    head(vec)
    head(vec,4)
    
    # tail of the Vector
    tail(vec)
    tail(vec,4)
    

    4. Conclusion

    In this article, you have learned what head() and tail() functions do in R. The head() function is used to get the first rows and the tail is used to get the last rows of objects DataFrame, Vector, Matrix, and List.

    <a id="h-related-articles"></a>

    Related Articles