Description Usage Arguments Details Value Author(s) See Also Examples
Computes the correlation coefficients between y
and x
if these are vectors. If y
and x
are matrices then the correlation between the columns of y
and the columns of x
are computed. This functionality is the same as cor
in stats except that it adds the formula notation y~x
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
x |
A numeric vector, matrix, or data.frame (see details in |
... |
Unused. |
y |
A numeric vector, matrix, or data.frame (see details in |
use |
An optional character string that defines a method for computing the correlation in the presence of missing values. This must be one of |
method |
A single character string that indicates which correlation coefficient is to be computed. Must be one of |
digits |
A single numeric that indicates the number of decimals to which the result should be rounded. |
data |
An optional data frame that contains the variables in the formula. |
see details in cor
. The formula version is only used with two variables in either the y~x
or ~y+x
form.
A single numeric if x
and y
are vectors or a numeric matrix if x
and y
are matrices.
Derek H. Ogle, derek@derekogle.com, but this is largely a wrapper to cor
from stats that includes a formula version.
See cor
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ## example from stats::cor() ... all return equivalent
df <- data.frame(x=1:10,y=2:11)
cor(df$x,df$y)
corr(df$x,df$y)
corr(y~x,data=df)
corr(~x+y,data=df)
corr(df)
## another example from stats::cor()
cor(longley)
corr(longley)
corr(longley[1:2],longley[3:5])
## handling missing values
set.seed(3473) # to control randomness
df <- data.frame(x=c(runif(9),NA),y=c(NA,runif(9)),z=runif(10),w=runif(10))
corr(~x+y,data=df)
corr(~x+y,data=df,use="complete.obs")
corr(~x+y+z,data=df,use="complete.obs")
corr(~x+y+z,data=df,use="pairwise.complete.obs")
# no messages below (as no missing values)
corr(~w+z,data=df,use="complete.obs")
corr(~w+z,data=df,use="pairwise.complete.obs")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.