Gunter
> Cc: r-help@r-project.org; s...@gnu.org
> Subject: Re: [R] cor() on sets of vectors
>
> On Thu, Feb 23, 2012 at 3:24 PM, Bert Gunter wrote:
> > Use 1:n as an index.
> >
> > e.g.
> > sapply(1:n, function(i) cor(x[,i],y[,i]))
>
> ## sapply is a good solution
Elai:
Thank you.You make an excellent point. cor() is implemented at the C
level (via a .internal call) whereas sapply implements an interpreted
loop that has to issue the call each time through the loop (with some
shortcuts/tricks to reduce overhead). So the operations count of the
original poste
On Thu, Feb 23, 2012 at 3:24 PM, Bert Gunter wrote:
> Use 1:n as an index.
>
> e.g.
> sapply(1:n, function(i) cor(x[,i],y[,i]))
## sapply is a good solution (the only one I could think of too), but
not always worth it:
# for 100 x 1000
x <- data.frame(matrix(rnorm(10),nc=1000))
y <- data.f
Use 1:n as an index.
e.g.
sapply(1:n, function(i) cor(x[,i],y[,i]))
-- Bert
On Thu, Feb 23, 2012 at 2:10 PM, Sam Steingold wrote:
> suppose I have two sets of vectors: x1,x2,...,xN and y1,y2,...,yN.
> I want N correlations: cor(x1,y1), cor(x2,y2), ..., cor(xN,yN).
> my sets of vectors are arr
sapply(1:NCOL(x), function(n) cor(x[n], y[n])) is a quick and dirty
way, though probably not optimal.
Michael
On Thu, Feb 23, 2012 at 5:10 PM, Sam Steingold wrote:
> suppose I have two sets of vectors: x1,x2,...,xN and y1,y2,...,yN.
> I want N correlations: cor(x1,y1), cor(x2,y2), ..., cor(xN,yN
suppose I have two sets of vectors: x1,x2,...,xN and y1,y2,...,yN.
I want N correlations: cor(x1,y1), cor(x2,y2), ..., cor(xN,yN).
my sets of vectors are arranged as data frames x & y (vector=column):
x <- data.frame(a=rnorm(10),b=rnorm(10),c=rnorm(10))
y <- data.frame(d=rnorm(10),e=rnorm(10),f=
6 matches
Mail list logo