# Create a Correlation Matrix in R

## Your goal

You need to create a correlation matrix in R.

## Step-by-step tutorial

Use the cor function from the stats package to calculate the correlation matrix.

> library(stats)
> precip.df <- read.csv("precip-central-park.csv")
> head(precip.df)
YEAR  JAN  FEB  MAR  APR  MAY  JUN  JUL  AUG  SEP  OCT  NOV  DEC ANNUAL
1 1869 2.53 6.87 4.61 1.39 4.15 4.40 3.20 1.76 2.81 6.48 2.03 5.02  45.25
2 1870 4.41 2.83 3.33 5.11 1.83 2.82 3.76 3.07 2.52 4.97 2.42 2.18  39.25
3 1871 2.07 2.72 5.54 3.03 4.04 7.05 5.57 5.60 2.34 7.50 3.56 2.24  51.26
4 1872 1.88 1.29 3.74 2.29 2.68 2.93 7.83 6.29 2.95 3.35 4.08 3.18  42.49
5 1873 5.34 3.80 2.09 4.16 3.69 1.28 4.61 9.56 3.14 2.73 4.63 2.96  47.99
6 1874 5.33 2.04 2.12 8.77 2.24 2.78 5.06 2.43 8.24 1.70 2.30 2.82  45.83
> cor(precip.df)
YEAR         JAN         FEB         MAR          APR
YEAR    1.000000000  0.03051119 -0.10112497  0.11114920  0.194853762
JAN     0.030511192  1.00000000 -0.06821696 -0.10262590  0.111274493
FEB    -0.101124967 -0.06821696  1.00000000 -0.02506027 -0.082795482
MAR     0.111149200 -0.10262590 -0.02506027  1.00000000  0.216825319
APR     0.194853762  0.11127449 -0.08279548  0.21682532  1.000000000
MAY     0.232004432  0.03012705  0.02182865 -0.02176157  0.085648357
JUN     0.197471179 -0.08567240  0.05814686  0.02698897  0.055813612
JUL     0.007274254 -0.14623455 -0.06761195 -0.05609089  0.050941695
AUG     0.008247778  0.05770505 -0.01276202  0.04175081  0.079984331
SEP     0.079724658 -0.01166484 -0.03898647  0.01800133 -0.002175506
OCT     0.069104191  0.03529907  0.02723136  0.14757930  0.175281442
NOV     0.143468677 -0.11890713 -0.05530723 -0.02670724 -0.012372570
DEC     0.214377461  0.03476941  0.04685952  0.13264203  0.257741374
ANNUAL  0.294144302  0.13770823  0.12475636  0.32114477  0.467535379
MAY         JUN          JUL          AUG          SEP
YEAR    0.232004432  0.19747118  0.007274254  0.008247778  0.079724658
JAN     0.030127047 -0.08567240 -0.146234555  0.057705049 -0.011664835
FEB     0.021828649  0.05814686 -0.067611946 -0.012762024 -0.038986467
MAR    -0.021761567  0.02698897 -0.056090890  0.041750812  0.018001334
APR     0.085648357  0.05581361  0.050941695  0.079984331 -0.002175506
MAY     1.000000000  0.10384985 -0.004012085  0.058314559  0.050845189
JUN     0.103849850  1.00000000  0.151932352 -0.023827304 -0.031939994
JUL    -0.004012085  0.15193235  1.000000000 -0.017260813  0.030097054
AUG     0.058314559 -0.02382730 -0.017260813  1.000000000  0.068532499
SEP     0.050845189 -0.03193999  0.030097054  0.068532499  1.000000000
OCT    -0.080315158  0.13499512 -0.042856425  0.120381852 -0.071483801
NOV     0.009115462  0.01799978  0.121552191 -0.023199095 -0.055560664
DEC     0.143836449  0.14197420  0.085505000  0.045182394 -0.033719653
ANNUAL  0.331190254  0.37692174  0.304247302  0.426935025  0.305504817
OCT          NOV         DEC    ANNUAL
YEAR    0.06910419  0.143468677  0.21437746 0.2941443
JAN     0.03529907 -0.118907135  0.03476941 0.1377082
FEB     0.02723136 -0.055307233  0.04685952 0.1247564
MAR     0.14757930 -0.026707235  0.13264203 0.3211448
APR     0.17528144 -0.012372570  0.25774137 0.4675354
MAY    -0.08031516  0.009115462  0.14383645 0.3311903
JUN     0.13499512  0.017999777  0.14197420 0.3769217
JUL    -0.04285643  0.121552191  0.08550500 0.3042473
AUG     0.12038185 -0.023199095  0.04518239 0.4269350
SEP    -0.07148380 -0.055560664 -0.03371965 0.3055048
OCT     1.00000000  0.048812925  0.08106591 0.4369432
NOV     0.04881292  1.000000000 -0.02995620 0.2322702
DEC     0.08106591 -0.029956204  1.00000000 0.4292934
ANNUAL  0.43694318  0.232270179  0.42929342 1.0000000

That's a pretty big correlation matrix. We can subset it to the first six months if we like:

> cor(precip.df[2:7])
JAN         FEB         MAR         APR         MAY         JUN
JAN  1.00000000 -0.06821696 -0.10262590  0.11127449  0.03012705 -0.08567240
FEB -0.06821696  1.00000000 -0.02506027 -0.08279548  0.02182865  0.05814686
MAR -0.10262590 -0.02506027  1.00000000  0.21682532 -0.02176157  0.02698897
APR  0.11127449 -0.08279548  0.21682532  1.00000000  0.08564836  0.05581361
MAY  0.03012705  0.02182865 -0.02176157  0.08564836  1.00000000  0.10384985
JUN -0.08567240  0.05814686  0.02698897  0.05581361  0.10384985  1.00000000