First glimpse

Packages: dplyr, psych, skimr, Hmisc, esmtools


After importing your dataset or while preprocessing it, it is important to have a quick and efficient overview of your dataset. To this end, we demonstrate how to display meta-data (e.g., dimensions), to display a subset of rows, to compute common descriptive statistics (e.g., mean, standard deviation) and to compute occurences of values. Beyond providing a good understanding of your dataset, it often a reveals some issues in the dataset, for example wrong minimal or maximal values, loss of many rows after a data manipulation, or an unexpected high number of occurrences for a value of a categorical variable.

Meta-data

Three important meta-data aspects to check are the number of rows and columns, the format of the columns, and the number of observations per participant. It can be done using:

  • dim(): returns the dimensions of the dataset: the number of rows (first number) and the number of columns (second number). It helps to quickly see if those numbers are the expected ones (e.g., after data modification). You can also investigate independently the number of rows with nrow() and the number of columns with ncol().

dim(data)
[1] 4200   18
  • str(): returns the columns’ formats and their first values. It is particularly useful to inspect if variables are in the correct formats (e.g., integer, character, POSIXct). This can also be done using glimpse() from the dplyr package.

str(data)
'data.frame':   4200 obs. of  18 variables:
 $ dyad     : num  1 1 1 1 1 1 1 1 1 1 ...
 $ role     : int  1 1 1 1 1 1 1 1 1 1 ...
 $ obsno    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ id       : num  1 1 1 1 1 1 1 1 1 1 ...
 $ age      : int  40 40 40 40 40 40 40 40 40 40 ...
 $ cond_dyad: chr  "condB" "condB" "condB" "condB" ...
 $ scheduled: POSIXct, format: "2018-10-17 08:00:08" "2018-10-17 09:00:01" ...
 $ sent     : POSIXct, format: "2018-10-17 08:00:11" "2018-10-17 09:00:22" ...
 $ start    : POSIXct, format: NA NA ...
 $ end      : POSIXct, format: NA NA ...
 $ contact  : int  NA NA NA 0 NA NA 0 0 0 NA ...
 $ PA1      : int  NA NA NA 1 NA NA 1 1 1 NA ...
 $ PA2      : int  NA NA NA 11 NA NA 1 1 1 NA ...
 $ PA3      : int  NA NA NA 25 NA NA 5 7 16 NA ...
 $ NA1      : int  NA NA NA 10 NA NA 30 30 43 NA ...
 $ NA2      : int  NA NA NA 16 NA NA 1 13 23 NA ...
 $ NA3      : int  NA NA NA 28 NA NA 35 41 46 NA ...
 $ location : chr  NA NA NA "A" ...
  • Number of rows per participant: with base R functions or dplyr functions. In the dplyr version, n() is meant to compute the number of rows for each group, so here for each participant.

by(data, data$id, nrow)
data$id: 1
[1] 70
------------------------------------------------------------ 
data$id: 2
[1] 70
------------------------------------------------------------ 
data$id: 3
[1] 70
------------------------------------------------------------ 
data$id: 4
[1] 70
------------------------------------------------------------ 
data$id: 5
[1] 70
------------------------------------------------------------ 
data$id: 6
[1] 70
------------------------------------------------------------ 
data$id: 7
[1] 70
------------------------------------------------------------ 
data$id: 8
[1] 70
------------------------------------------------------------ 
data$id: 9
[1] 70
------------------------------------------------------------ 
data$id: 10
[1] 70
------------------------------------------------------------ 
data$id: 11
[1] 70
------------------------------------------------------------ 
data$id: 12
[1] 70
------------------------------------------------------------ 
data$id: 13
[1] 70
------------------------------------------------------------ 
data$id: 14
[1] 70
------------------------------------------------------------ 
data$id: 15
[1] 70
------------------------------------------------------------ 
data$id: 16
[1] 70
------------------------------------------------------------ 
data$id: 17
[1] 70
------------------------------------------------------------ 
data$id: 18
[1] 70
------------------------------------------------------------ 
data$id: 19
[1] 70
------------------------------------------------------------ 
data$id: 20
[1] 70
------------------------------------------------------------ 
data$id: 21
[1] 70
------------------------------------------------------------ 
data$id: 22
[1] 70
------------------------------------------------------------ 
data$id: 23
[1] 70
------------------------------------------------------------ 
data$id: 24
[1] 70
------------------------------------------------------------ 
data$id: 25
[1] 70
------------------------------------------------------------ 
data$id: 26
[1] 70
------------------------------------------------------------ 
data$id: 27
[1] 70
------------------------------------------------------------ 
data$id: 28
[1] 70
------------------------------------------------------------ 
data$id: 29
[1] 70
------------------------------------------------------------ 
data$id: 30
[1] 70
------------------------------------------------------------ 
data$id: 31
[1] 70
------------------------------------------------------------ 
data$id: 32
[1] 70
------------------------------------------------------------ 
data$id: 33
[1] 70
------------------------------------------------------------ 
data$id: 34
[1] 70
------------------------------------------------------------ 
data$id: 35
[1] 70
------------------------------------------------------------ 
data$id: 36
[1] 70
------------------------------------------------------------ 
data$id: 37
[1] 70
------------------------------------------------------------ 
data$id: 38
[1] 70
------------------------------------------------------------ 
data$id: 39
[1] 70
------------------------------------------------------------ 
data$id: 40
[1] 70
------------------------------------------------------------ 
data$id: 41
[1] 70
------------------------------------------------------------ 
data$id: 42
[1] 70
------------------------------------------------------------ 
data$id: 43
[1] 70
------------------------------------------------------------ 
data$id: 44
[1] 70
------------------------------------------------------------ 
data$id: 45
[1] 70
------------------------------------------------------------ 
data$id: 46
[1] 70
------------------------------------------------------------ 
data$id: 47
[1] 70
------------------------------------------------------------ 
data$id: 48
[1] 70
------------------------------------------------------------ 
data$id: 49
[1] 70
------------------------------------------------------------ 
data$id: 50
[1] 70
------------------------------------------------------------ 
data$id: 51
[1] 70
------------------------------------------------------------ 
data$id: 52
[1] 70
------------------------------------------------------------ 
data$id: 53
[1] 70
------------------------------------------------------------ 
data$id: 54
[1] 70
------------------------------------------------------------ 
data$id: 55
[1] 70
------------------------------------------------------------ 
data$id: 56
[1] 70
------------------------------------------------------------ 
data$id: 57
[1] 70
------------------------------------------------------------ 
data$id: 58
[1] 70
------------------------------------------------------------ 
data$id: 59
[1] 70
------------------------------------------------------------ 
data$id: 60
[1] 70

Display rows

A direct inspection of the observations/rows can also be useful to perform a quick investigation in many situations, such as after importing data or creating new variables. Most common practices are to use head() and ’tail()` to display the first and last rows of the dataset respectively. However, only displaying the first or last subset of rows of a dataset may not provide a good representation of it, and, worse, it may obfuscate problematic patterns, outliers, or other data quality issues. Hence, displaying a set of random rows may be a good alternative.

  • head() and ’tail(): return, respectively, the first and last n rows (by default n = 5). We can change n as follows: head(data, n = 10) or tail(data, n = 10).

head(data, n = 5) 
  dyad role obsno id age cond_dyad           scheduled                sent
1    1    1     1  1  40     condB 2018-10-17 08:00:08 2018-10-17 08:00:11
2    1    1     2  1  40     condB 2018-10-17 09:00:01 2018-10-17 09:00:22
3    1    1     3  1  40     condB 2018-10-17 09:59:56 2018-10-17 10:00:08
4    1    1     4  1  40     condB 2018-10-17 10:59:48 2018-10-17 10:59:52
5    1    1     5  1  40     condB 2018-10-17 12:00:12 2018-10-17 12:00:15
                start                 end contact PA1 PA2 PA3 NA1 NA2 NA3
1                <NA>                <NA>      NA  NA  NA  NA  NA  NA  NA
2                <NA>                <NA>      NA  NA  NA  NA  NA  NA  NA
3                <NA>                <NA>      NA  NA  NA  NA  NA  NA  NA
4 2018-10-17 11:00:12 2018-10-17 11:03:01       0   1  11  25  10  16  28
5                <NA>                <NA>      NA  NA  NA  NA  NA  NA  NA
  location
1     <NA>
2     <NA>
3     <NA>
4        A
5     <NA>
  • Random rows: in the esmtools package, you can find three functions that display:
    • (1) random rows: with the function randrows(), you can display n randomly selected rows from the dataset.
    • (2) a random set of following rows: with the function folrows(), you can displays one randomly selected sets of n following rows.
    • (3) multiple random sets of following rows: with the function folrows(), you can display nb_sample randomly selected sets of n following rows.

library(esmtools)
randrows(data, n = 5)
     dyad role obsno id age cond_dyad           scheduled                sent
1720   13    1    40 25  25     condB 2018-07-25 11:59:57 2018-07-25 12:00:02
92      1    2    22  2  42     condB 2018-10-21 09:00:09 2018-10-21 09:00:16
3888   28    2    38 56  48     condA 2018-11-03 10:59:58 2018-11-03 10:59:59
2935   21    2    65 42  28     condA 2018-06-14 11:59:51 2018-06-14 11:59:54
2680   20    1    20 39  25     condA 2018-07-03 11:59:56 2018-07-03 12:00:13
                   start                 end contact PA1 PA2 PA3 NA1 NA2 NA3
1720                <NA>                <NA>      NA  NA  NA  NA  NA  NA  NA
92   2018-10-21 09:00:35 2018-10-21 09:01:26       0  19   1   1   1  12  88
3888 2018-11-03 11:00:10 2018-11-03 11:00:51       0  23  14   1   1   5  90
2935                <NA>                <NA>      NA  NA  NA  NA  NA  NA  NA
2680                <NA>                <NA>      NA  NA  NA  NA  NA  NA  NA
     location
1720     <NA>
92          A
3888        E
2935     <NA>
2680     <NA>

Common descriptive functions

Many packages offer functions that can be used to compute descriptive statistics (e.g., mean, median, quantiles, proportion of missing values) over one or multiple variables of a dataframe. Computing descriptive statistics is an useful way to check some assumptions about the data (e.g., range of values), to detect potential issues (e.g., important number of missing values), or to have a quick overview of the data. Here, we show five functions coming from different packages:

summary(data)
      dyad           role         obsno            id             age       
 Min.   : 1.0   Min.   :1.0   Min.   : 1.0   Min.   : 1.00   Min.   :25.00  
 1st Qu.: 8.0   1st Qu.:1.0   1st Qu.:18.0   1st Qu.:15.75   1st Qu.:25.00  
 Median :15.5   Median :1.5   Median :35.5   Median :30.50   Median :35.50  
 Mean   :15.5   Mean   :1.5   Mean   :35.5   Mean   :30.50   Mean   :35.10  
 3rd Qu.:23.0   3rd Qu.:2.0   3rd Qu.:53.0   3rd Qu.:45.25   3rd Qu.:42.25  
 Max.   :30.0   Max.   :2.0   Max.   :70.0   Max.   :60.00   Max.   :65.00  
                                                                            
  cond_dyad           scheduled                     
 Length:4200        Min.   :2018-02-02 08:59:47.00  
 Class :character   1st Qu.:2018-07-20 09:00:01.00  
 Mode  :character   Median :2018-09-13 11:00:13.50  
                    Mean   :2018-09-08 11:33:24.83  
                    3rd Qu.:2018-10-24 02:59:52.50  
                    Max.   :2019-06-10 11:59:46.00  
                    NA's   :2                       
      sent                            start                       
 Min.   :2018-02-02 08:59:51.00   Min.   :2018-02-02 09:00:31.00  
 1st Qu.:2018-07-20 09:00:18.75   1st Qu.:2018-07-22 12:00:37.25  
 Median :2018-09-13 11:30:18.00   Median :2018-09-14 22:00:28.00  
 Mean   :2018-09-08 11:54:09.94   Mean   :2018-09-14 18:49:31.13  
 3rd Qu.:2018-10-23 17:00:04.00   3rd Qu.:2018-10-31 13:00:30.50  
 Max.   :2019-06-10 11:59:54.00   Max.   :2019-06-10 12:00:15.00  
                                  NA's   :1254                    
      end                            contact            PA1        
 Min.   :2018-02-02 09:03:07.00   Min.   :0.0000   Min.   :  1.00  
 1st Qu.:2018-07-22 12:01:49.25   1st Qu.:0.0000   1st Qu.:  4.00  
 Median :2018-09-14 22:02:02.00   Median :0.0000   Median : 18.00  
 Mean   :2018-09-14 18:51:14.74   Mean   :0.1229   Mean   : 23.09  
 3rd Qu.:2018-10-31 13:02:31.00   3rd Qu.:0.0000   3rd Qu.: 32.00  
 Max.   :2019-06-10 12:02:30.00   Max.   :1.0000   Max.   :100.00  
 NA's   :1254                     NA's   :1254     NA's   :1254    
      PA2              PA3              NA1              NA2      
 Min.   :  1.00   Min.   :  1.00   Min.   :  1.00   Min.   : 1.0  
 1st Qu.:  3.00   1st Qu.:  3.00   1st Qu.:  1.00   1st Qu.: 1.0  
 Median : 19.00   Median : 16.00   Median : 11.00   Median : 7.0  
 Mean   : 21.77   Mean   : 23.32   Mean   : 21.36   Mean   :10.5  
 3rd Qu.: 33.00   3rd Qu.: 31.00   3rd Qu.: 31.00   3rd Qu.:15.0  
 Max.   :100.00   Max.   :100.00   Max.   :100.00   Max.   :83.0  
 NA's   :1254     NA's   :1254     NA's   :1254     NA's   :1254  
      NA3           location        
 Min.   :  1.00   Length:4200       
 1st Qu.: 40.00   Class :character  
 Median : 72.00   Mode  :character  
 Mean   : 63.72                     
 3rd Qu.: 89.00                     
 Max.   :100.00                     
 NA's   :1254                       

Occurences of values

Finally, we can also be interested in the occurrences of values for the specific items, particularly with Likert scales or multiple choice questions. In such a case, it is useful to investigate the number of unique values and inspect if some values have unexpectedly high or low numbers of occurrences. We can either display:

  • the overall occurrences: numbers of occurences for each value over the whole data set.
  • the within-participant occurrences: numbers of occurences for each value and for each participant in the data set. In the output of the function, the rows are the participants and the columns are the different values of the item.

# Overall occurrences
table(data$location, useNA="ifany")

   A    B    C    D    E <NA> 
 621  543  535  608  639 1254 
# Within-participant occurrences
table(data$id, data$location, useNA="ifany")
    
      A  B  C  D  E <NA>
  1  11  9  4 10  7   29
  2  14 14  4 12 10   16
  3   4  4  8  8 12   34
  4   8 10 10 16  8   18
  5   4  4  8  4 11   39
  6   6  8 10  6  8   32
  7   6  8  7 15 11   23
  8   9  8 12  9 13   19
  9  13  4  2  2  3   46
  10  7  3  9 11  9   31
  11  8 11 11 10 17   13
  12 13 14 10  9 14   10
  13 13 10  8 12 12   15
  14 17 10 11 13 11    8
  15 11 11 10 10 14   14
  16  7 11 12 11 16   13
  17  5  3  9  4 13   36
  18 12  5 11 15 14   13
  19  9  7 12 12 14   16
  20 22  6 13 11 14    4
  21  3  4  7 10  8   38
  22  4  7  9  8 13   29
  23 13  8  7  9  8   25
  24 13  8  8 11 10   20
  25  9 12  4  4  9   32
  26 20 12 13  8 16    1
  27 13 13  7  8 10   19
  28 11 12 13 11 10   13
  29  3  9  7 11 10   30
  30 20 10 16  8 13    3
  31 11 16  5 12 12   14
  32  9 16 12  8 11   14
  33 11 14  8 12  9   16
  34 14  6 10 15 13   12
  35  8 11  7 14 13   17
  36 10  8 16 14 15    7
  37  7 11  9 11  7   25
  38 17 11  3 13  8   18
  39 12  5  6 10  6   31
  40 13 15 17  6 12    7
  41  8 12  9 12  6   23
  42  7  8  7  7 12   29
  43  8  9  6 11  9   27
  44 13  7  7  7 12   24
  45 19  8  5 13  8   17
  46 13 11 13 11 11   11
  47 11 11 11  8 11   18
  48  8 10  9 13 13   17
  49 10  4  4  7 13   32
  50 11 12  7 16  4   20
  51  3  6  6  7  7   41
  52  9  9  7  9  4   32
  53 10 11  2  7  5   35
  54 12 13 14  7  9   15
  55 14  8 17 13 11    7
  56 13  6  8 20 18    5
  57  8 11 13  5  7   26
  58  6  9 12 10 12   21
  59  5  2  4  7 12   40
  60 13  8  9 15 11   14