12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

$ Employed : num 60.3 61.1 60.2 61.2 63.2 ...One way to easily analyze the different variables of a data frame is to firstreshape the data frame from wide format to tall format.A wide data frame contains a column for each variable (see Chapter 13). Atall data frame contains all the same information, but the data is organized insuch a way that one column is reserved for identifying the name of thevariable and a second column contains the actual data.An easy way to reshape a data frame from wide format to tall format is to usethe melt() function in the reshape2 package. Remember: reshape2 is not part ofbase R — it’s an add-on package that is available on CRAN. You can install it withthe install.packages(“reshape2”) function.> library(“reshape2”)> mlongley str(mlongley)‘data.frame’: 96 obs. of 3 variables:$ Year : int 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 ...$ variable: Factor w/ 6 levels “GNP.deflator”,..: 1 1 1 1 1 1 1 1 1 1 ...$ value : num 83 88.5 88.2 89.5 96.2 ...Now you can plot the tall data frame mlongley and use the new columns valueand variable in the formula value~Year | variable.> xyplot(value ~ Year | variable, data=mlongley,+ layout=c(6, 1),+ par.strip.text=list(cex=0.7),+ scales=list(cex=0.7)+ )The additional arguments par.strip.text and scales control the font size(character expansion ratio) of the strip at the top of the chart, as well as the scale,as you can see in Figure 17-9.When you create plots with multiple groups, make sure that the resultingplot is meaningful. For example, Figure 17-9 correctly plots the longley data,but it can be very misleading because the units of measurement are verydifferent. For example, the unit of GNP (short for Gross National Product) isprobably billions of dollars. In contrast the unit of population is probably

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!