Working with output objects

R For SAS Users

Melinda Higgins, PhD

Research Professor/Senior Biostatistician Emory University

Output from summary

# Save summary output, view davissmry
davissmry <- daviskeep %>%
  select(weight, height, bmi) %>%
  summary()
# Display summary output object
davissmry
     weight          height           bmi       
 Min.   : 39.0   Min.   :148.0   Min.   :15.82  
 1st Qu.: 55.0   1st Qu.:164.0   1st Qu.:20.22  
 Median : 63.0   Median :170.0   Median :21.80  
 Mean   : 65.3   Mean   :170.6   Mean   :22.26  
 3rd Qu.: 73.5   3rd Qu.:177.5   3rd Qu.:23.94  
 Max.   :119.0   Max.   :197.0   Max.   :36.73
R For SAS Users

Output from summary

# Class of davissmry
class(davissmry)
# Check davissmry is also a matrix
is.matrix(davissmry)
# Display dimensions of davissmry
dim(davissmry)

[1] "table"

[1] TRUE

[1] 6 3
R For SAS Users

Select elements from summary object

# Display columns 1 to 2 from output
davissmry[,1:2]
     weight          height     
 Min.   : 39.0   Min.   :148.0  
 1st Qu.: 55.0   1st Qu.:164.0  
 Median : 63.0   Median :170.0  
 Mean   : 65.3   Mean   :170.6  
 3rd Qu.: 73.5   3rd Qu.:177.5  
 Max.   :119.0   Max.   :197.0
# Display rows 4 to 6 from output
davissmry[4:6,]
     weight          height           bmi       
 Mean   : 65.3   Mean   :170.6   Mean   :22.26  
 3rd Qu.: 73.5   3rd Qu.:177.5   3rd Qu.:23.94  
 Max.   :119.0   Max.   :197.0   Max.   :36.73
R For SAS Users

Output from dplyr summarise

# Save output from summarise
davissmall <- daviskeep %>%
  select(weight, height) %>%
  summarise(across(everything(), 
                   list(mean = ~mean(.x),
                        sd = ~sd(.x))))
# Display davissmall output object
davissmall
  weight_mean weight_sd height_mean height_sd
1    65.29648  13.34346    170.5879  8.948848
# Show structure of davissmall object
str(davissmall)
'data.frame':    1 obs. of  4 variables:
 $ weight_mean: num 65.3
 $ weight_sd  : num 13.3
 $ height_mean: num 171
 $ height_sd  : num 8.95
R For SAS Users

Display elements from summarise output

# Display column 3 for mean height
davissmall[, 3]
[1] 170.5879
# Display element name height_mean
davissmall$height_mean
[1] 170.5879
R For SAS Users

Output from dplyr group_by

# Use group_by get stats for bmi by sex
davisbmisex <- daviskeep %>%
  group_by(sex) %>%
  select(bmi, sex) %>%
  summarise(across(everything(), 
                   list(mean = ~mean(.x),
                        sd = ~sd(.x))))
# Display output object
davisbmisex
# A tibble: 2 × 3
  sex   bmi_mean bmi_sd
  <fct>    <dbl>  <dbl>
1 F         21.0   2.18
2 M         23.9   3.12
# Get structure of davisbmisex
str(davisbmisex)
tibble [2 × 3] (S3: tbl_df/tbl/data.frame)
 $ sex     : Factor w/ 2 levels "F","M": 1 2
 $ bmi_mean: num [1:2] 21 23.9
 $ bmi_sd  : num [1:2] 2.18 3.12
1 The sex variable was automatically coded as a Factor class type.
R For SAS Users

Display elements from group_by output

# Display column 2 of davisbmisex
davisbmisex[, 2]
# A tibble: 2 × 1
  bmi_mean
     <dbl>
1     21.0
2     23.9
# Display row 1 of davisbmisex
davisbmisex[1,]
# A tibble: 1 × 3
  sex   bmi_mean bmi_sd
  <fct>    <dbl>  <dbl>
1 F         21.0   2.18
R For SAS Users

Display elements from group_by output

# Display sd element from davisbmisex
davisbmisex$bmi_sd
[1] 2.176094 3.120285
# Display row for males
davisbmisex %>%
  filter(sex == "M")
# A tibble: 1 × 3
  sex   bmi_mean bmi_sd
  <fct>    <dbl>  <dbl>
1 M         23.9   3.12
R For SAS Users

Output from psych describe

# Save psych::describe() output as davispsych
davispsych <- daviskeep %>%
  select(weight, height, bmi) %>%
  psych::describe()
# Display davispsych output object
davispsych
       vars   n   mean    sd median trimmed   mad    min    max range skew kurtosis   se
weight    1 199  65.30 13.34   63.0   64.12 11.86  39.00 119.00 80.00 0.91     0.84 0.95
height    2 199 170.59  8.95  170.0  170.40 10.38 148.00 197.00 49.00 0.21    -0.38 0.63
bmi       3 199  22.26  3.01   21.8   22.08  2.55  15.82  36.73 20.91 0.91     1.91 0.21
R For SAS Users

Class and structure of psych describe output

str(davispsych)
Classes ‘psych’, ‘describe’ and 'data.frame':
     3 obs. of  13 variables:
 $ vars    : int  1 2 3
 $ n       : num  199 199 199
 $ mean    : num  65.3 170.6 22.3
 $ sd      : num  13.34 8.95 3.01
 $ median  : num  63 170 21.8
 $ trimmed : num  64.1 170.4 22.1
 $ mad     : num  11.86 10.38 2.55
 $ min     : num  39 148 15.8
 $ max     : num  119 197 36.7
 $ range   : num  80 49 20.9
 $ skew    : num  0.905 0.21 0.913
 $ kurtosis: num  0.843 -0.379 1.914
 $ se      : num  0.946 0.634 0.213
R For SAS Users

Display psych describe output elements

# Display trimmed means
davispsych$trimmed
[1]  64.12422 170.40373  22.08452
# Display row 2 for height
davispsych %>%
  slice(2)
  vars   n   mean   sd median trimmed   mad min max range skew kurtosis   se
1    2 199 170.59 8.95    170   170.4 10.38 148 197    49 0.21    -0.38 0.63
R For SAS Users

Display psych describe output elements

# Display sample size, median, min and max elements
davispsych %>%
  select(n, median, min, max)
         n median    min    max
weight 199   63.0  39.00 119.00
height 199  170.0 148.00 197.00
bmi    199   21.8  15.82  36.73
R For SAS Users

Let's customize the results for the statistical output on abalones!

R For SAS Users

Preparing Video For Download...