Mostrando entradas con la etiqueta psych. Mostrar todas las entradas
Mostrando entradas con la etiqueta psych. 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

2020-03-05

Descriptive statistics by group in R

Title

Problem

We'd like to report descriptive statistics in R by a grouping variable and subsetting the output statistics.

Solution

We will use the data frame iris, columns Sepal.Length and Sepal.Width and grouping by Species. In our example, we want to return the mean, the standard deviation, the skewness and kurtosis.

  • Subset of descriptive statistics by group
  • library(psych)
    # Variables by index
    d <- describeBy(iris[1:2], group = iris$Species)
    # Two options to subset the statistics:
    lapply(d, "[", , c(3, 4, 11, 12))
    lapply(d, subset, , c(3, 4, 11, 12)) 
    
    # Variables by name
    i <- match(c("Sepal.Length", "Petal.Length"), names(iris))
    d <- describeBy(iris[i], group = iris$Species)
    lapply(d, subset, , c("mean", "sd", "skew", "kurtosis")) 
    
    $setosa
                 mean   sd skew kurtosis
    Sepal.Length 5.01 0.35 0.11    -0.45
    Sepal.Width  3.43 0.38 0.04     0.60
    
    $versicolor
                 mean   sd  skew kurtosis
    Sepal.Length 5.94 0.52  0.10    -0.69
    Sepal.Width  2.77 0.31 -0.34    -0.55
    
    $virginica
                 mean   sd skew kurtosis
    Sepal.Length 6.59 0.64 0.11    -0.20
    Sepal.Width  2.97 0.32 0.34     0.38
    
  • Subset of descriptive statistics without grouping
  • # Seleccionamos las columnas deseadas de la tabla
    d <- describe(iris[1:2])
    # Subsetting output statistics
    d[, c(3, 4, 11, 12)]
    
                 mean   sd skew kurtosis
    Sepal.Length 5.84 0.83 0.31    -0.61
    Sepal.Width  3.06 0.44 0.31     0.14
    

    References

    2020-03-04

    Descriptive statistics in R

    Title

    Problem

    We'd like to compute descriptive statistics in R.

    Solution

  • The summary function returns a set of summary statistics for the input (a vector, data frame or model).
  • # For a variable
    summary(iris$Sepal.Length)
    
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      4.300   5.100   5.800   5.843   6.400   7.900 
    
    # For a data frame
    summary(iris)
    
      Sepal.Length    Sepal.Width     Petal.Length    Petal.Width          Species  
     Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   setosa    :50  
     1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   versicolor:50  
     Median :5.800   Median :3.000   Median :4.350   Median :1.300   virginica :50  
     Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199                  
     3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800                  
     Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
    
  • The fivenum function returns Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input data.
  • # For a variable
    fivenum(iris$Sepal.Width)
    
    [1] 2.0 2.8 3.0 3.3 4.4
    
  • The boxplot.stats function returns the statistics necessary for producing box plots.
  • boxplot.stats(iris$Sepal.Width)
    
    $stats
    [1] 2.2 2.8 3.0 3.3 4.0
    
    $n
    [1] 150
    
    $conf
    [1] 2.935497 3.064503
    
    $out
    [1] 4.4 4.1 4.2 2.0
    
    To return a specific statistic we type boxplot.stats(iris$Sepal.Width) followed by:

    $stats - vector with Tukey's five number summary.
    $n - the number of non-NA observations.
    $conf - the lower and upper extremes of the ‘notch’.
    $out- outliers.

    The psych package

  • For a data frame
  • library(psych)
    describe(iris)
    
                 vars   n mean   sd median trimmed  mad
    Sepal.Length    1 150 5.84 0.83   5.80    5.81 1.04
    Sepal.Width     2 150 3.06 0.44   3.00    3.04 0.44
    Petal.Length    3 150 3.76 1.77   4.35    3.76 1.85
    Petal.Width     4 150 1.20 0.76   1.30    1.18 1.04
    Species*        5 150  NaN   NA     NA     NaN   NA
                 min  max range  skew kurtosis   se
    Sepal.Length 4.3  7.9   3.6  0.31    -0.61 0.07
    Sepal.Width  2.0  4.4   2.4  0.31     0.14 0.04
    Petal.Length 1.0  6.9   5.9 -0.27    -1.42 0.14
    Petal.Width  0.1  2.5   2.4 -0.10    -1.36 0.06
    Species*     Inf -Inf  -Inf    NA       NA   NA
    
  • Statistics by group
  • describeBy(iris, group = iris$Species)
    
    group: setosa
                 vars  n mean   sd median trimmed  mad
    Sepal.Length    1 50 5.01 0.35    5.0    5.00 0.30
    Sepal.Width     2 50 3.43 0.38    3.4    3.42 0.37
    Petal.Length    3 50 1.46 0.17    1.5    1.46 0.15
    Petal.Width     4 50 0.25 0.11    0.2    0.24 0.00
    Species*        5 50  NaN   NA     NA     NaN   NA
                 min  max range skew kurtosis   se
    Sepal.Length 4.3  5.8   1.5 0.11    -0.45 0.05
    Sepal.Width  2.3  4.4   2.1 0.04     0.60 0.05
    Petal.Length 1.0  1.9   0.9 0.10     0.65 0.02
    Petal.Width  0.1  0.6   0.5 1.18     1.26 0.01
    Species*     Inf -Inf  -Inf   NA       NA   NA
    --------------------------------------- 
    group: versicolor
                 vars  n mean   sd median trimmed  mad
    Sepal.Length    1 50 5.94 0.52   5.90    5.94 0.52
    Sepal.Width     2 50 2.77 0.31   2.80    2.78 0.30
    Petal.Length    3 50 4.26 0.47   4.35    4.29 0.52
    Petal.Width     4 50 1.33 0.20   1.30    1.32 0.22
    Species*        5 50  NaN   NA     NA     NaN   NA
                 min  max range  skew kurtosis   se
    Sepal.Length 4.9  7.0   2.1  0.10    -0.69 0.07
    Sepal.Width  2.0  3.4   1.4 -0.34    -0.55 0.04
    Petal.Length 3.0  5.1   2.1 -0.57    -0.19 0.07
    Petal.Width  1.0  1.8   0.8 -0.03    -0.59 0.03
    Species*     Inf -Inf  -Inf    NA       NA   NA
    --------------------------------------- 
    group: virginica
                 vars  n mean   sd median trimmed  mad
    Sepal.Length    1 50 6.59 0.64   6.50    6.57 0.59
    Sepal.Width     2 50 2.97 0.32   3.00    2.96 0.30
    Petal.Length    3 50 5.55 0.55   5.55    5.51 0.67
    Petal.Width     4 50 2.03 0.27   2.00    2.03 0.30
    Species*        5 50  NaN   NA     NA     NaN   NA
                 min  max range  skew kurtosis   se
    Sepal.Length 4.9  7.9   3.0  0.11    -0.20 0.09
    Sepal.Width  2.2  3.8   1.6  0.34     0.38 0.05
    Petal.Length 4.5  6.9   2.4  0.52    -0.37 0.08
    Petal.Width  1.4  2.5   1.1 -0.12    -0.75 0.04
    Species*     Inf -Inf  -Inf    NA       NA   NA
    

    2018-12-27

    Obtener datos geoespaciales por países con R: elevación

    Problema

    En la entrada anterior obtuvimos las divisiones administrativas con el paquete raster en R. En esta ocasión extraeremos datos de altitud o elevación del terreno.

    Solución

    Empleamos la función getData del paquete raster. Necesitamos suministrar el argumento country con un código ISO de 3 letras.

    library(raster)
    # Países disponibles
    library(psych) # Función HeadTail 
    headTail(getData('ISO3'))
    
        ISO3                  NAME
    1    AFG           Afghanistan
    2    XAD Akrotiri and Dhekelia
    3    ALA                 Åland
    4    ALB               Albania
    ...                   
    253  ESH        Western Sahara
    254  YEM                 Yemen
    255  ZMB                Zambia
    256  ZWE              Zimbabwe
    
    Suministramos el argumento 'alt' (altitude). Computamos slope y aspect con la función terrain para poder suministrarlos posteriormente a la función hillShade.

    alt <-  getData('alt', country = 'ESP')
    slope <-  terrain(alt, opt = 'slope')
    aspect <-  terrain(alt, opt = 'aspect')
    hill <-  hillShade(slope, aspect, 40, 270)
    
    library(tmap)
    tm_shape(hill) +
      tm_raster(palette = gray(0:100 / 100),
                n = 100,
                legend.show = FALSE)  +
      tm_shape(alt) +
      tm_raster(alpha = 0.5,
                palette = terrain.colors(25),
                legend.show = FALSE)
    

    Mapa

    Entradas relacionadas

    Referencias

    2018-12-25

    Obtener datos geoespaciales por países con R: divisiones administrativas

    Problema

    Deseamos obtener datos espaciales de las áreas administrativas de un país con el paquete raster de R. Con estos datos, SpatialPolygonDataFrame (polígonos espaciales en un data frame), podremos dibujar mapas en R.

    Solución

    Empleamos la función getData del paquete raster. Necesitamos suministrar el argumento country con un código ISO de 3 letras.

    library(raster)
    # Países disponibles
    library(psych) # Función HeadTail 
    headTail(getData('ISO3'))
    
        ISO3                  NAME
    1    AFG           Afghanistan
    2    XAD Akrotiri and Dhekelia
    3    ALA                 Åland
    4    ALB               Albania
    ...                   
    253  ESH        Western Sahara
    254  YEM                 Yemen
    255  ZMB                Zambia
    256  ZWE              Zimbabwe
    
    El primer argumento es 'GADM' que es una base de datos de divisiones administrativas. También necesitamos especificar el nivel de la subdivisión administrativa (0 = país, 1 = primer nivel). Por ejemplo, para España: 0 = país, 1 = comunidades autónomas, 2 = provincias, 3 = comarcas, 4 = municipios.

    # Descargamos el nivel 4
    df4 <- getData("GADM", country = "ES", level = 4)
    View(df4)
    
    Vista de la pestaña Environment tras importar todos los niveles.

    Mapas

    Representamos las divisiones administrarivas del nivel 1. Separamos península y Baleares de Canarias para apreciar mejor los mapas.

    df11 <- df1[df1@data$NAME_1 != "Islas Canarias",]
    df12 <- df1[df1@data$NAME_1 == "Islas Canarias",]
    plot(df11, main = "Península y Baleares - Divisiones administrativas nivel 1")
    plot(df12, main = "Canarias- Divisiones administrativas nivel 1")
    

    Descarga manual

    Tammbién podemos acceder y descargar manualmente los ficheros de los diferentes niveles aquí.

    Entradas relacionadas

    Referencias

    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