Re: [R] Correlation Matrix

From: David Winsemius <>
Date: Thu, 07 Apr 2011 15:26:59 -0400

On Apr 7, 2011, at 3:09 PM, Dmitry Berman wrote:

> Listers,
> I have a question regarding correlation matrices. It is fairly
> straight
> forward to build a correlation matrix of an entire data frame. I
> simply use
> the command cor(MyDataFrame). However, what I would like to do is
> construct
> a smaller correlation matrix using just three of the variable out of
> my data
> set.
> When I run this:
> cor(MyDataFrame$variable1, MyDataFrame$variable2,MyDataFrame
> $variable3) I
> get an error.

But you choose not to offer it. Why is that?

> Is there a way to do this through a built in function or is this
> something I
> have to construct manually?

Neither. You need to learn how R does argument matching:

 From help(cor)
cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman"))"

So your 3 unnamed arguments were matched by position to `x`, `y` and `use`.
Try cor(x = myDataFrame[, c("variable1", "variable2", "variable3")]


David Winsemius, MD
West Hartford, CT

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 07 Apr 2011 - 19:28:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Apr 2011 - 19:50:27 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive