The following is a basic analysis of mtcars, a data set preloaded in R.

Summary of the data

The command summary(mtcars) will summarize the data, detailing the mean, media and max & min values of each variable:

summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

Description of the Data

To calculate the standard deviation and other descriptions of the data, install the package ‘psych’ and then run the command describe(mtcars). The result will include information about the standard deviation as well as the skew and distribution of the data.

require(psych)
## Loading required package: psych
describe(mtcars) 
##      vars  n   mean     sd median trimmed    mad   min    max  range  skew
## mpg     1 32  20.09   6.03  19.20   19.70   5.41 10.40  33.90  23.50  0.61
## cyl     2 32   6.19   1.79   6.00    6.23   2.97  4.00   8.00   4.00 -0.17
## disp    3 32 230.72 123.94 196.30  222.52 140.48 71.10 472.00 400.90  0.38
## hp      4 32 146.69  68.56 123.00  141.19  77.10 52.00 335.00 283.00  0.73
## drat    5 32   3.60   0.53   3.70    3.58   0.70  2.76   4.93   2.17  0.27
## wt      6 32   3.22   0.98   3.33    3.15   0.77  1.51   5.42   3.91  0.42
## qsec    7 32  17.85   1.79  17.71   17.83   1.42 14.50  22.90   8.40  0.37
## vs      8 32   0.44   0.50   0.00    0.42   0.00  0.00   1.00   1.00  0.24
## am      9 32   0.41   0.50   0.00    0.38   0.00  0.00   1.00   1.00  0.36
## gear   10 32   3.69   0.74   4.00    3.62   1.48  3.00   5.00   2.00  0.53
## carb   11 32   2.81   1.62   2.00    2.65   1.48  1.00   8.00   7.00  1.05
##      kurtosis    se
## mpg     -0.37  1.07
## cyl     -1.76  0.32
## disp    -1.21 21.91
## hp      -0.14 12.12
## drat    -0.71  0.09
## wt      -0.02  0.17
## qsec     0.34  0.32
## vs      -2.00  0.09
## am      -1.92  0.09
## gear    -1.07  0.13
## carb     1.26  0.29

Creating Variables

Variables can be created and manipulated using R. Use the ‘<-’ symbol as an ‘equal to’ indicator.

x <- 2 * 2
y <- x + 10 
z <- x / y

Arithmatic Functions with the Variables

Now that values have been assigned to the variables, they can be incorporated into functions which R will execute. R can also solve for varaibles with the command “solve()”

zz <-(x+1)*(y-1)
solve(x)
##      [,1]
## [1,] 0.25


This analysis was powered by R:

Here is a “chunk” of R code with a comment in it:

100/10 #This math I could do in my head
## [1] 10

Here is R code with the code hidden, which is accomplished with the echo = FALSE command. The code is the round command applied to mtcars and taken to one digit.

##                      mpg cyl  disp  hp drat  wt qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110  3.9 2.6 16.5  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110  3.9 2.9 17.0  0  1    4    4
## Datsun 710          22.8   4 108.0  93  3.8 2.3 18.6  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110  3.1 3.2 19.4  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175  3.1 3.4 17.0  0  0    3    2
## Valiant             18.1   6 225.0 105  2.8 3.5 20.2  1  0    3    1
## Duster 360          14.3   8 360.0 245  3.2 3.6 15.8  0  0    3    4
## Merc 240D           24.4   4 146.7  62  3.7 3.2 20.0  1  0    4    2
## Merc 230            22.8   4 140.8  95  3.9 3.1 22.9  1  0    4    2
## Merc 280            19.2   6 167.6 123  3.9 3.4 18.3  1  0    4    4
## Merc 280C           17.8   6 167.6 123  3.9 3.4 18.9  1  0    4    4
## Merc 450SE          16.4   8 275.8 180  3.1 4.1 17.4  0  0    3    3
## Merc 450SL          17.3   8 275.8 180  3.1 3.7 17.6  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180  3.1 3.8 18.0  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205  2.9 5.2 18.0  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215  3.0 5.4 17.8  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230  3.2 5.3 17.4  0  0    3    4
## Fiat 128            32.4   4  78.7  66  4.1 2.2 19.5  1  1    4    1
## Honda Civic         30.4   4  75.7  52  4.9 1.6 18.5  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65  4.2 1.8 19.9  1  1    4    1
## Toyota Corona       21.5   4 120.1  97  3.7 2.5 20.0  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150  2.8 3.5 16.9  0  0    3    2
## AMC Javelin         15.2   8 304.0 150  3.1 3.4 17.3  0  0    3    2
## Camaro Z28          13.3   8 350.0 245  3.7 3.8 15.4  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175  3.1 3.8 17.1  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66  4.1 1.9 18.9  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91  4.4 2.1 16.7  0  1    5    2
## Lotus Europa        30.4   4  95.1 113  3.8 1.5 16.9  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264  4.2 3.2 14.5  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175  3.6 2.8 15.5  0  1    5    6
## Maserati Bora       15.0   8 301.0 335  3.5 3.6 14.6  0  1    5    8
## Volvo 142E          21.4   4 121.0 109  4.1 2.8 18.6  1  1    4    2