Re: [Rd] cor() and cor.test() (PR#9781)

From: Greg Snow <Greg.Snow_at_intermountainmail.org>
Date: Thu, 05 Jul 2007 14:40:09 -0600


The cor function does not know how to look inside of data frames (unless you give it the entire data frame as the only argument). If Pollution and Wet.days are columns of the data frame named Pollution (which I infer from your problem statement below) then you can do things like:

> cor(Pollution$Pollution, Pollution$Wed.days)

Or

> cor( Pollution[ ,c('Pollution','Wet.days')] )

Or

> with( Pollution, cor(Pollution, Wet.days) )

Or

> attach(Pollution)
> cor(Pollution, Wet.days)
> detach()

The last one may have problems since the data frame has the same name as the column. The with option is prefered to the last one anyways.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow_at_intermountainmail.org
(801) 408-8111
 
 


> -----Original Message-----
> From: r-devel-bounces_at_r-project.org
> [mailto:r-devel-bounces_at_r-project.org] On Behalf Of
> kristle.krichbaum_at_gmail.com
> Sent: Thursday, July 05, 2007 1:49 PM
> To: r-devel_at_stat.math.ethz.ch
> Cc: R-bugs_at_biostat.ku.dk
> Subject: [Rd] cor() and cor.test() (PR#9781)
>
> Hello,
>
> I am trying to make a correlation matrix in R using cor() and
> also to get the p-value for each correlation using
> cor.test(). I can't get these commands to work. I'm getting
> errors like the following:
>
> cor(Pollution, Wet.days)
> Error in inherits(x, "data.frame") : Object "Wet.days" not
> found cor("Pollution", "Wet.days") Error in cor("Pollution",
> "Wet.days") : missing observations in cov/cor In addition:
> Warning messages:
> 1: NAs introduced by coercion
> 2: NAs introduced by coercion
>
> I know that "Wet.days" is there because when I type the name
> of the data set
> (Pollution) it appears. There are no missing values in the
> data set for there to be any NAs coerced. This is the example
> given in Mick Crawley's book on R with Pollution data so it
> should definitely work.
>
> Can you help me to get it working, please?
> Kristle
>
> --
> Kristle Krichbaum
> Graduate Assistant
> Hudson Lab
> 208 Mueller Lab
> University Park, PA 16801
> www.cidd.psu.edu
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> ______________________________________________ R-devel_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 05 Jul 2007 - 21:01:27 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 Fri 06 Jul 2007 - 11:35:48 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.