[R] help writing for loop

Steven McKinney smckinney at bccrc.ca
Wed Nov 25 20:40:07 CET 2009


Hi,

The great thing about the S language is that it is 'vectorized',
so you can do a lot of matrix manipulations without loops.

Here's a smaller example of what you describe.
matrix A with 3 columns and 10 rows (instead of 100)
matrix B with 3 columns and 15 rows (instead of 1500)

> set.seed(123)
> mA <- matrix(runif(30), ncol = 3)
> mA
           [,1]       [,2]      [,3]
 [1,] 0.2875775 0.95683335 0.8895393
 [2,] 0.7883051 0.45333416 0.6928034
 [3,] 0.4089769 0.67757064 0.6405068
 [4,] 0.8830174 0.57263340 0.9942698
 [5,] 0.9404673 0.10292468 0.6557058
 [6,] 0.0455565 0.89982497 0.7085305
 [7,] 0.5281055 0.24608773 0.5440660
 [8,] 0.8924190 0.04205953 0.5941420
 [9,] 0.5514350 0.32792072 0.2891597
[10,] 0.4566147 0.95450365 0.1471136

> x <- seq(15)
> mB <- cbind(1, x, x^2)
> mB
         x    
 [1,] 1  1   1
 [2,] 1  2   4
 [3,] 1  3   9
 [4,] 1  4  16
 [5,] 1  5  25
 [6,] 1  6  36
 [7,] 1  7  49
 [8,] 1  8  64
 [9,] 1  9  81
[10,] 1 10 100
[11,] 1 11 121
[12,] 1 12 144
[13,] 1 13 169
[14,] 1 14 196
[15,] 1 15 225

> mR <- apply(mB, 1, function(x){mA %*% x})
> mR
          [,1]     [,2]      [,3]      [,4]      [,5]     [,6]     [,7]
 [1,] 2.133950 5.759401 11.163931 18.347540 27.310227 38.05199 50.57284
 [2,] 1.934443 4.466187  8.383538 13.686496 20.375061 28.44923 37.90901
 [3,] 1.727054 4.326145  8.206250 13.367368 19.809500 27.53265 36.53681
 [4,] 2.449921 6.005363 11.549346 19.081867 28.602929 40.11253 53.61067
 [5,] 1.699098 3.769140  7.150594 11.843459 17.847736 25.16342 33.79052
 [6,] 1.653912 4.679328  9.121806 14.981344 22.257943 30.95160 41.06232
 [7,] 1.318259 3.196545  6.162963 10.217513 15.360195 21.59101 28.90995
 [8,] 1.528621 3.353106  6.365876 10.566930 15.956267 22.53389 30.29979
 [9,] 1.168515 2.363915  4.137635  6.489674  9.420032 12.92871 17.01571
[10,] 1.558232 2.954077  4.644149  6.628448  8.906974 11.47973 14.34671
          [,8]     [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
 [1,] 64.87276 80.95176  98.80984 118.44700 139.86324 163.05856 188.03295
 [2,] 48.75440 60.98539  74.60199  89.60419 105.99201 123.76542 142.92445
 [3,] 46.82198 58.38816  71.23536  85.36358 100.77281 117.46305 135.43430
 [4,] 69.09735 86.57257 106.03633 127.48863 150.92947 176.35884 203.77676
 [5,] 43.72904 54.97896  67.54029  81.41304  96.59720 113.09277 130.89975
 [6,] 52.59011 65.53495  79.89685  95.67582 112.87184 131.48493 151.51508
 [7,] 37.31703 46.81224  57.39559  69.06706  81.82667  95.67440 110.61027
 [8,] 39.25398 49.39646  60.72722  73.24626  86.95358 101.84919 117.93309
 [9,] 21.68102 26.92466  32.74662  39.14689  46.12549  53.68240  61.81763
[10,] 17.50792 20.96335  24.71302  28.75691  33.09502  37.72737  42.65394
          [,15]
 [1,] 214.78642
 [2,] 163.46908
 [3,] 154.68657
 [4,] 233.18322
 [5,] 150.01814
 [6,] 172.96229
 [7,] 126.63428
 [8,] 135.20527
 [9,]  70.53119
[10,]  47.87474
> 

so the results matrix mR has one column for each element of x,
and the i-th row element is the solution for
y = a + bx + cx^2
for the i-th row of coefficients in the parameter estimates matrix mA

You can spot check with e.g. 

> mA[1,] %*% mB[15,]
         [,1]
[1,] 214.7864
> mA[1,] %*% mB[5,]
         [,1]
[1,] 27.31023
> mA[1,]
[1] 0.2875775 0.9568333 0.8895393
> mB[5,]
    x    
 1  5 25 
> 



HTH

Steven McKinney, Ph.D.

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: smckinney +at+ bccrc +dot+ ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada

________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Jessica Schedlbauer [jschedlb at fiu.edu]
Sent: November 25, 2009 10:43 AM
To: r-help at r-project.org
Subject: [R] help writing for loop

Hi,

I’d like to ask for some help in writing a loop.  My situation is the following:

I have a matrix (matrix.A) containing 3 columns and 100 rows.  The columns represent parameter estimates a, b, and c.  The rows contain different values for these parameter estimates.  Each row is unique.

I want to insert these parameter estimates into a model (say, y = a + bx + cx^2) and solve for y given a separate matrix (matrix.B) of x values (where x has a length of 1500).

I want to solve for y 100 times using each set of the parameter estimates in matrix.A once.

At present my code looks like this and it only performs the first iteration.

For (i in 1:length(matrix.A)) { y <- matrix.A$a[[i]] + matrixA$b[[i]] * matrix.B$x + matrixA$c[[i]] * matrix.B$x^2)

I have not been able to figure out how to loop through the rows of parameter estimates in matrix.A.  I am new to writing loops, so any assistance would be much appreciated.

Regards,
Jessica Schedlbauer

______________________________________________
R-help at 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.




More information about the R-help mailing list