How to Create a Data Frame from Scratch in R
How to Identify and Correct Dropped Dimensions in R
How to Evaluate the Differences in Your Data with R

How to Test Data Normality Graphically in R

You could, of course, plot a histogram with R for every data sample you want to look at. You can use the histogram() function pretty easily to plot histograms for different groups.

Using the formula interface, you can plot two histograms at once using the following code:

image0.png
> library(lattice)
> histogram(~temp | factor(activ), data=beaver2)

The histogram() function uses a one-sided formula, so you don’t specify anything at the left side of the tilde (~). On the right side, you specify the following:

  • Which variable the histogram should be created for: In this case, that’s the variable temp, containing the body temperature.

  • After the vertical line (|),the factor by which the data should be split: In this case, that’s the variable activ that has a value 1 if the beaver was active and 0 if it was not. To convert this numeric variable to a factor, you use the factor() function.

The vertical line (|) in the formula interface can be read as “conditional on.” It’s used in that context in the formula interfaces of more advanced statistical functions as well.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Juggle Dimensions and Replace Values in a Matrix in R
How to Do Basic Arithmetic in R
How to Search the Web for Help with R
How to Install, Load, and Unload Packages in R
How to Look at the Structure of a Factor in R
Advertisement

Inside Dummies.com