Mostrando entradas con la etiqueta tail. Mostrar todas las entradas
Mostrando entradas con la etiqueta tail. Mostrar todas las entradas

2020-03-07

Show the first and last n lines of a data frame in R

Problem

We want to show the first and last n lines of a data frame. The equivalent of using the head and tail functions at the same time.

Solution

We can use the function headTail from the psych package.

  1. Defatult options
  2. By default headTail returns the top 4 and bottom 4 lines separated with dots (ellipsis).

    library(psych)
    headTail(ToothGrowth)
    
         len supp dose
    1    4.2   VC  0.5
    2   11.5   VC  0.5
    3    7.3   VC  0.5
    4    5.8   VC  0.5
    ...  ...   ...
    57  26.4   OJ    2
    58  27.3   OJ    2
    59  29.4   OJ    2
    60    23   OJ    2
    
  3. Arguments
  4. We can control tne number of lines to show at the top and the bottom, the ellipsis (how top and bottom are separared), number of columns to show and round the number of digits. Some examples:

    # The first and last 2 lines without separation
    headTail(ToothGrowth, top = 2, bottom = 2, ellipsis = FALSE)
    
        len supp dose
    1   4.2   VC  0.5
    2  11.5   VC  0.5
    59 29.4   OJ  2.0
    60 23.0   OJ  2.0
    
    # The first and last 3, from column 4 to 5 with no decimals.
    headTail(iris, top = 3, bottom = 3, digits = 0 , from = 4, to = 5)
    
       Petal.Width   Species
    1             0    setosa
    2             0    setosa
    3             0    setosa
    ...         ...      
    148           2 virginica
    149           2 virginica
    150           2 virginica
    

Related posts

References

2018-03-31

Mostrar las primeras y últimas n filas de un data frame en R

Problema

Deseamos mostrar simultáneamente las primeras n y últimas n filas de una matriz o data frame. Es decir, combinar las funciones head y tail.

Solución

La función headTail del paquete psych nos permite visualizar simultáneamente las n primeras filas (head) y las últimas n filas (tail).

  1. Opciones por defecto
  2. Por defecto headTail muestra las 4 primeras y últimas filas del objeto separadas por puntos.

    library(psych)
    headTail(ToothGrowth)
    
         len supp dose
    1    4.2   VC  0.5
    2   11.5   VC  0.5
    3    7.3   VC  0.5
    4    5.8   VC  0.5
    ...  ...   ...
    57  26.4   OJ    2
    58  27.3   OJ    2
    59  29.4   OJ    2
    60    23   OJ    2
    
  3. Más opciones
  4. La función headTail nos permite controlar el número de filas de la parte superior e inferior, la separación entre ambas, las columnas a mostrar y redondear el número dígitos. Veamos algunos ejemplos:

    #Primeras y últimas dos filas sin separación
    headTail(ToothGrowth, top = 2, bottom = 2, ellipsis = FALSE)
    
        len supp dose
    1   4.2   VC  0.5
    2  11.5   VC  0.5
    59 29.4   OJ  2.0
    60 23.0   OJ  2.0
    
    #Primeras y últimas tres filas, las columnas 4 y 5, con separación y sin decimales.
    headTail(iris, top = 3, bottom = 3, digits = 0 , from = 4, to = 5)
    
       Petal.Width   Species
    1             0    setosa
    2             0    setosa
    3             0    setosa
    ...         ...      
    148           2 virginica
    149           2 virginica
    150           2 virginica
    

Entradas relacionadas

Nube de datos