This is a basic introductory look at using R for generating descriptive statistics of a univariate data set. Here, we will use the historical dataset of Michelson’s experiment to determine the speed of light in air provided as a an ASCII file with header content and the observed speed of light for 100 trials.
We need to first read the data into R. Since the data is in a properly formatted ASCII file, we only need to tell R to ignore the first 60 lines, which is header information. R will then import the data into a list of class data.frame.
>C <- read.table("Michelso.dat",skip=60)
We can take a look at the dataset by simply typing the dataset name at the prompt. Here you can see that R automatically assigned the variable V1 to the data.
The summary() command in R provides the summary statistics: MIn, 1st Q, Median, Mean, 3rd Q and Max. We call this function with the argument 'C$V1' which tells R to act on the named variable, V1, in the data.frame C. (The options commands set the output number formatting to something realistic.)
Min. 1st Qu. Median Mean 3rd Qu. Max.
299.6200 299.8075 299.8500 299.8524 299.8925 300.0700
Standard deviation, trimmed mean and number of data points can be obtained individually.
If we want to get skewness and kurtosis we'll need the fBasics package installed
To determine confidence intervals on the mean, we can use the one sample t-test. We can ignore the mean value to test against since in our case it is not known (or relevant for confidence interval estimation)
> t.test(C$V1, conf.level=0.99)
One Sample t-test
t = 37950.9329, df = 99, p-value < 0.00000000000000022
alternative hypothesis: true mean is not equal to 0
99 percent confidence interval:
mean of x
Another method for obtaining much of this information in a single step can be found in the stat.desc() function from the pastecs package.
We'll look at the generation of some standard statistical plots for exploratory data analysis in a future post.
Caveat lector — All work and ideas presented here may not be accurate and should be verified before application.