# Scales of Measurement

| September 22nd, 2014

## Converting a distribution to Z-scale

In this exercise, you will convert the ratings for the Australian red wine to the Z-scale. The data are already loaded into your workspace as `ratings_australia`.

R provides you with an easy way to do this. Given a list of observations, you can transform these observations to Z-scores using the `scale()` function.

### Instructions

• Convert the ratings stored in `ratings_australia` to Z-scores and assign the result to `z_scores_australia`.
• Plot the histograms for the original ratings and the Z-scores next to each other. The command `par(mfrow = c(1, 2))` tells R to show your graphics in a matrix with 1 row and 2 columns, or side-by-side. As always, make sure that your histograms have sensible titles and clear axis labels.
```library(psych) library(sm) red_wine_data = read.table(url("http://assets.datacamp.com/course/Conway/Lecture_Data/L15-16_Wine_Example.txt"), header = T) ratings_australia = subset(red_wine_data, red_wine_data\$condition == "Australia")\$Ratings``` ```## ratings_australia is already loaded # Print the ratings for the Australian red wine ratings_australia # Convert these ratings to Z-scores using the scale() function z_scores_australia <- ___ # Arrange the histograms side-by-side par(mfrow = c(1, 2)) # Plot the histogram for the original scores # Plot the histogram for the Z-scores ``` ```## ratings_australia is already loaded # Print the ratings for the Australian red wine ratings_australia # Convert these ratings to Z-scores using the scale() function z_scores_australia <- scale(ratings_australia) # Arrange the histograms side-by-side par(mfrow = c(1, 2)) # Plot the histogram for the original scores hist(ratings_australia, main = "Australia red wine ratings", xlab = "score") # Plot the histogram for the Z-scores hist(z_scores_australia, main = "Scaled ratings", xlab = "Z-score")``` ``` test_object("z_scores_australia", incorrect_msg = "It looks like you did not assign the correct Z-scores to <code>z_scores_australia</code>. Use the <code>scale()</code> function to do this. If you don't know how this function works, you can type <code>?scale</code> to get the help file.") test_function("hist", index = 1, args = c("x"), incorrect_msg = "Did you plot a histogram of the original red wine ratings of Australia?") test_function("hist", index = 2, args = c("x"), incorrect_msg = "Did you plot a histogram of the Z-scores?") success_msg("Well done! Take a look at both histograms and make sure that you understand they represent the same data, just on a different scale.") ```
• Use the `scale()` function to convert the original ratings to Z-scores.
• Use the `hist()` function to plot the histograms. Look back at the chapter on histograms and distributions if you have forgotten how to do this.