Filter(is.numeric,birth_dat)Summarizing Numerical Data
We will use the births data set to summarize and visualize numerical variables using the base R approach
Numerical data refers to data that can be measured and expressed as a number, such as age, height, weight, and temperature. Numerical data can be discrete or continuous
Single numerical variable
One way to extract all the numerical columns is using both Filter and is.numeric functions. Below are the first five rows of all the numerical columns in birth_dat
fage mage weeks visits gained weight
1 NA 13 39 10 38 7.63
2 NA 14 42 15 20 7.88
3 19 15 37 11 38 6.63
4 21 15 41 6 34 8.00
5 NA 15 39 9 27 6.38
The names of the numerical columns can be obtained using colnames() function in combination with the above statement
colnames(Filter(is.numeric,birth_dat) )[1] "fage" "mage" "weeks" "visits" "gained" "weight"
We will only consider the weight variable from our dataset to demonstrate methods to summarize and visualize a numerical variable.
Functions for numerical summaries include, but not limited to,
| Function | Description |
|---|---|
mean() |
mean |
median() |
median |
mode() |
mode |
sd() |
standard deviation |
var() |
variance |
min() |
minimum |
max() |
maximum |
summary() |
Computes the following: Minimum ,1st Quartile, Median,Mean ,3rd Quartile,Maximum |
Next, we’ll save the values from weight column into a separate variable and compute several numerical summaries listed above
birth_weight <- birth_dat$weightmean(birth_weight)[1] 7.101
median(birth_weight)[1] 7.31
min(birth_weight)[1] 1
max(birth_weight)[1] 11.75
summary(birth_weight) Min. 1st Qu. Median Mean 3rd Qu. Max.
1.000 6.380 7.310 7.101 8.060 11.750
While summary() give us a quick numerical summary of our distribution, it is important to also visualize the overall distribution using a plot such as a boxplot
boxplot(birth_weight)
or a histogram
hist(birth_weight)
From the histogram above, while the overall distribution of the birth weights is symmetrical there are outliers causing the distribution to be skewed to the left.
For boxplot() and hist() we used the default settings, while they are informative we can alter their appearance to be more professional.
For example, we changed the x-axis label and y-axis label using xlab,ylab arguments,respectively. We changed the title with main and the color of the boxplot with col. The col argument can take values such as red,blue or any HEX code, see ?boxplot for further customization.
boxplot(birth_weight,
main='Boxplot of Birth Weights',
xlab ='birth weights', ylab='ounces',
col='#61b1ed')
Search ‘color picker’ in Google and copy/paste the generated hex code
We can apply similar customization to our histogram
hist(birth_weight,
main='Histogram of Birth Weights',
xlab ='birth weights',
col='#d1584f',
breaks=20)
The hist function uses the Sturges method by default to determine the number of breaks on the histogram. We can manually change the number of breaks, but we should be careful not to specify a low or high number of breaks. Usually the default setting is appropriate for most scenarios.