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. Is there a way to do this through a built in function or is this something I have to construct manually? Thanks [[alternative HTML version deleted]]
Hi Dmitry, You might try with(MyDataFrame[, 1:3]) is variable1, variable2 and variable3 correspond to the first three columns of your data, or with( MyDataFrame( cor( cbind( variable1, variable2, variable3) ) ) ) otherwise. HTH, Jorge On Thu, 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. > > Is there a way to do this through a built in function or is this something > I > have to construct manually? > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 7 April 2011 12:09, Dmitry Berman <ravenblur at gmail.com> 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. > > Is there a way to do this through a built in function or is this something I > have to construct manually? >You can use cbind(). cor(cbind(MyDataFrame$variable1, MyDataFrame$variable2,MyDataFrame$variable3) ) Jeremy
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) "Usage: 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