[R] tagging results of "apply"

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jul 22 13:23:49 CEST 2007


You don't need apply at all here.  cor can already do that and it
automatically labels the rows and columns too.  Using the builtin
dataset anscombe whose first 4 columns are labelled x1,x2,x3,x4
and whose next 4 columns are labelled y1,y2,y3,y4 we have:

> cor(anscombe[1:4], anscombe[5:8])
           y1         y2         y3         y4
x1  0.8164205  0.8162365  0.8162867 -0.3140467
x2  0.8164205  0.8162365  0.8162867 -0.3140467
x3  0.8164205  0.8162365  0.8162867 -0.3140467
x4 -0.5290927 -0.7184365 -0.3446610  0.8165214

cor works the same with matrices too.


On 7/20/07, Bernzweig, Bruce (Consultant) <bbernzwe at bear.com> wrote:
> In trying to get a better understanding of vectorization I wrote the
> following code:
>
> My objective is to take two sets of time series and calculate the
> correlations for each combination of time series.
>
> mat1 <- matrix(sample(1:500, 25), ncol = 5)
> mat2 <- matrix(sample(501:1000, 25), ncol = 5)
>
> Scenario 1:
> apply(mat1, 1, function(x) cor(mat1, mat2[1,]))
>
> Scenario 2:
> apply(mat1, 1, function(x) cor(mat1, mat2))
>
> Using scenario 1, (output below) I can see that correlations are
> calculated for just the first row of mat2 against each individual row of
> mat1.
>
> Using scenario 2, (output below) I can see that correlations are
> calculated for each row of mat2 against each individual row of mat1.
>
> Q1: The output of scenario2 consists of 25 rows of data.  Are the first
> five rows mat1 against mat2[1,], the next five rows mat1 against
> mat2[2,], ... last five rows mat1 against mat2[5,]?
>
> Q2: I assign the output of scenario 2 to a new matrix
>
>        matC <- apply(mat1, 1, function(x) cor(mat1, mat2))
>
>    However, I need a way to identify each row in matC as a pairing of
> rows from mat1 and mat2.  Is there a parameter I can add to apply to do
> this?
>
> Scenario 1:
> > apply(mat1, 1, function(x) cor(mat1, mat2[1,]))
>           [,1]       [,2]       [,3]       [,4]       [,5]
> [1,] -0.4626122 -0.4626122 -0.4626122 -0.4626122 -0.4626122
> [2,] -0.9031543 -0.9031543 -0.9031543 -0.9031543 -0.9031543
> [3,]  0.0735273  0.0735273  0.0735273  0.0735273  0.0735273
> [4,]  0.7401259  0.7401259  0.7401259  0.7401259  0.7401259
> [5,] -0.4548582 -0.4548582 -0.4548582 -0.4548582 -0.4548582
>
> Scenario 2:
> > apply(mat1, 1, function(x) cor(mat1, mat2))
>             [,1]        [,2]        [,3]        [,4]        [,5]
>  [1,]  0.19394126  0.19394126  0.19394126  0.19394126  0.19394126
>  [2,]  0.26402400  0.26402400  0.26402400  0.26402400  0.26402400
>  [3,]  0.12923842  0.12923842  0.12923842  0.12923842  0.12923842
>  [4,] -0.74549676 -0.74549676 -0.74549676 -0.74549676 -0.74549676
>  [5,]  0.64074122  0.64074122  0.64074122  0.64074122  0.64074122
>  [6,]  0.26931986  0.26931986  0.26931986  0.26931986  0.26931986
>  [7,]  0.08527921  0.08527921  0.08527921  0.08527921  0.08527921
>  [8,] -0.28034079 -0.28034079 -0.28034079 -0.28034079 -0.28034079
>  [9,] -0.15251915 -0.15251915 -0.15251915 -0.15251915 -0.15251915
> [10,]  0.19542415  0.19542415  0.19542415  0.19542415  0.19542415
> [11,]  0.75107032  0.75107032  0.75107032  0.75107032  0.75107032
> [12,]  0.53042767  0.53042767  0.53042767  0.53042767  0.53042767
> [13,] -0.51163612 -0.51163612 -0.51163612 -0.51163612 -0.51163612
> [14,] -0.44396048 -0.44396048 -0.44396048 -0.44396048 -0.44396048
> [15,]  0.57018745  0.57018745  0.57018745  0.57018745  0.57018745
> [16,]  0.70480284  0.70480284  0.70480284  0.70480284  0.70480284
> [17,] -0.36674283 -0.36674283 -0.36674283 -0.36674283 -0.36674283
> [18,] -0.81826607 -0.81826607 -0.81826607 -0.81826607 -0.81826607
> [19,]  0.53145184  0.53145184  0.53145184  0.53145184  0.53145184
> [20,]  0.24568385  0.24568385  0.24568385  0.24568385  0.24568385
> [21,] -0.10610402 -0.10610402 -0.10610402 -0.10610402 -0.10610402
> [22,] -0.78650748 -0.78650748 -0.78650748 -0.78650748 -0.78650748
> [23,]  0.04269423  0.04269423  0.04269423  0.04269423  0.04269423
> [24,]  0.14704698  0.14704698  0.14704698  0.14704698  0.14704698
> [25,]  0.28340166  0.28340166  0.28340166  0.28340166  0.28340166
>
>
>
> **********************************************************************
> Please be aware that, notwithstanding the fact that the pers...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list